1 : |
dgarcia |
1 |
/**
|
2 : |
|
|
* Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
|
3 : |
|
|
*
|
4 : |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
5 : |
|
|
* of this software and associated documentation files (the "Software"), to deal
|
6 : |
|
|
* in the Software without restriction, including without limitation the rights
|
7 : |
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8 : |
|
|
* copies of the Software, and to permit persons to whom the Software is
|
9 : |
|
|
* furnished to do so, subject to the following conditions:
|
10 : |
|
|
*
|
11 : |
|
|
* The above copyright notice and this permission notice shall be included in
|
12 : |
|
|
* all copies or substantial portions of the Software.
|
13 : |
|
|
*
|
14 : |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15 : |
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16 : |
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17 : |
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18 : |
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19 : |
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20 : |
|
|
* SOFTWARE.
|
21 : |
|
|
*/
|
22 : |
|
|
|
23 : |
|
|
package eu.smartlm.abs.portal.view.portlet;
|
24 : |
|
|
|
25 : |
|
|
import java.io.IOException;
|
26 : |
|
|
import java.text.DecimalFormat;
|
27 : |
|
|
import java.util.ArrayList;
|
28 : |
|
|
import java.util.Collections;
|
29 : |
|
|
import java.util.HashSet;
|
30 : |
|
|
import java.util.List;
|
31 : |
|
|
import java.util.Set;
|
32 : |
|
|
|
33 : |
|
|
import javax.portlet.ActionRequest;
|
34 : |
|
|
import javax.portlet.ActionResponse;
|
35 : |
|
|
import javax.portlet.Event;
|
36 : |
|
|
import javax.portlet.EventRequest;
|
37 : |
|
|
import javax.portlet.EventResponse;
|
38 : |
|
|
import javax.portlet.GenericPortlet;
|
39 : |
|
|
import javax.portlet.PortletException;
|
40 : |
|
|
import javax.portlet.PortletRequestDispatcher;
|
41 : |
|
|
import javax.portlet.PortletSession;
|
42 : |
|
|
import javax.portlet.RenderRequest;
|
43 : |
|
|
import javax.portlet.RenderResponse;
|
44 : |
|
|
|
45 : |
|
|
import com.liferay.portal.kernel.log.Log;
|
46 : |
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
47 : |
|
|
|
48 : |
|
|
import eu.smartlm.abs.portal.security.login.controller.LoginController;
|
49 : |
|
|
import eu.smartlm.abs.portal.util.StringUtil;
|
50 : |
|
|
import eu.smartlm.abs.portal.view.event.EventData;
|
51 : |
|
|
import eu.smartlm.abs.portal.view.portlet.model.SmartLMUsageRecordTypeSorted;
|
52 : |
|
|
import eu.smartlm.schemas.x2009.x06.urec.LicenseResourceType;
|
53 : |
|
|
import eu.smartlm.schemas.x2009.x06.urec.SmartLMUsageRecordType;
|
54 : |
|
|
|
55 : |
|
|
|
56 : |
|
|
/**
|
57 : |
|
|
* Shows the results of a query to the accounting and billing system. Both in graphs done by
|
58 : |
|
|
* JFreeChart and in a informative table.
|
59 : |
|
|
*
|
60 : |
|
|
* @author David Garcia Perez-CESGA
|
61 : |
|
|
*
|
62 : |
|
|
*/
|
63 : |
|
|
public class ResultPortlet extends GenericPortlet {
|
64 : |
|
|
private String type;
|
65 : |
|
|
public static String GRAPH_DONE = "graphDone";
|
66 : |
|
|
public static String GRAPH_DONE_BILLING = "graphDoneBilling";
|
67 : |
|
|
public static String GRAPH = "graph";
|
68 : |
|
|
public List<List<String>> usageInformationList = new ArrayList<List<String>>();
|
69 : |
|
|
protected String editJSP;
|
70 : |
|
|
protected String helpJSP;
|
71 : |
|
|
protected String viewJSP;
|
72 : |
|
|
private static Log _log = LogFactoryUtil.getLog(ResultPortlet.class);
|
73 : |
|
|
private DecimalFormat twoPlaces = new DecimalFormat("0.00");
|
74 : |
|
|
|
75 : |
|
|
private EventData eventData = new EventData(false, null, new HashSet<SmartLMUsageRecordType>(), type, "", "");
|
76 : |
|
|
|
77 : |
|
|
|
78 : |
|
|
@Override
|
79 : |
|
|
public void init() throws PortletException {
|
80 : |
|
|
editJSP = getInitParameter("edit-jsp");
|
81 : |
|
|
helpJSP = getInitParameter("help-jsp");
|
82 : |
|
|
viewJSP = getInitParameter("view-jsp");
|
83 : |
|
|
type = getInitParameter("type");
|
84 : |
|
|
}
|
85 : |
|
|
|
86 : |
|
|
@Override
|
87 : |
|
|
public void doDispatch(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
|
88 : |
|
|
|
89 : |
|
|
String jspPage = renderRequest.getParameter("jspPage");
|
90 : |
|
|
|
91 : |
|
|
if (jspPage != null) {
|
92 : |
|
|
include(jspPage, renderRequest, renderResponse);
|
93 : |
|
|
} else {
|
94 : |
|
|
super.doDispatch(renderRequest, renderResponse);
|
95 : |
|
|
}
|
96 : |
|
|
}
|
97 : |
|
|
|
98 : |
|
|
@Override
|
99 : |
|
|
public void doEdit(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
|
100 : |
|
|
|
101 : |
|
|
if (renderRequest.getPreferences() == null) {
|
102 : |
|
|
super.doEdit(renderRequest, renderResponse);
|
103 : |
|
|
} else {
|
104 : |
|
|
include(editJSP, renderRequest, renderResponse);
|
105 : |
|
|
}
|
106 : |
|
|
}
|
107 : |
|
|
|
108 : |
|
|
@Override
|
109 : |
|
|
public void doHelp(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
|
110 : |
|
|
|
111 : |
|
|
include(helpJSP, renderRequest, renderResponse);
|
112 : |
|
|
}
|
113 : |
|
|
|
114 : |
|
|
@Override
|
115 : |
|
|
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
|
116 : |
|
|
PortletSession session = renderRequest.getPortletSession();
|
117 : |
|
|
|
118 : |
|
|
// We show information if the user is logged in.
|
119 : |
|
|
int status = LoginController.LOGGED_OUT;
|
120 : |
|
|
Integer loginStatus = (Integer) session.getAttribute(LoginController.LOGIN_EVENT, PortletSession.APPLICATION_SCOPE);
|
121 : |
|
|
if(loginStatus != null) status = loginStatus.intValue();
|
122 : |
|
|
|
123 : |
|
|
session.setAttribute("usageInformationList", usageInformationList);
|
124 : |
|
|
if(eventData.isRepresent() && eventData.getGraph() != null && status == LoginController.LOGGED_IN) {
|
125 : |
|
|
session.setAttribute("graph", eventData.getGraph(), PortletSession.APPLICATION_SCOPE);
|
126 : |
|
|
session.setAttribute("eventpdf", eventData, PortletSession.APPLICATION_SCOPE);
|
127 : |
|
|
session.setAttribute("representGraph", true);
|
128 : |
|
|
session.setAttribute("eventdata", eventData);
|
129 : |
|
|
}
|
130 : |
|
|
else
|
131 : |
|
|
session.setAttribute("representGraph", false);
|
132 : |
|
|
|
133 : |
|
|
include(viewJSP, renderRequest, renderResponse);
|
134 : |
|
|
}
|
135 : |
|
|
|
136 : |
|
|
@Override
|
137 : |
|
|
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
|
138 : |
|
|
|
139 : |
|
|
}
|
140 : |
|
|
|
141 : |
|
|
@Override
|
142 : |
|
|
public void processEvent(EventRequest eventRequest, EventResponse eventResponse) {
|
143 : |
|
|
Event event = eventRequest.getEvent();
|
144 : |
|
|
|
145 : |
|
|
// We check if we need to represent the Graph.
|
146 : |
|
|
if(type.equals(QueryPortlet.TYPE_ACCOUNTING)) {
|
147 : |
|
|
if(event.getName().equals(ResultPortlet.GRAPH_DONE)) {
|
148 : |
|
|
eventData = (EventData) event.getValue();
|
149 : |
|
|
prepareUsageInformationList(type);
|
150 : |
|
|
}
|
151 : |
|
|
}
|
152 : |
|
|
else if (type.equals(QueryPortlet.TYPE_BILLING)) {
|
153 : |
|
|
if(event.getName().equals(ResultPortlet.GRAPH_DONE_BILLING)) {
|
154 : |
|
|
eventData = (EventData) event.getValue();
|
155 : |
|
|
double total = prepareUsageInformationList(type);
|
156 : |
|
|
String totalString = twoPlaces.format(total);
|
157 : |
|
|
eventData.setTotal(totalString);
|
158 : |
|
|
}
|
159 : |
|
|
}
|
160 : |
|
|
}
|
161 : |
|
|
|
162 : |
|
|
protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
|
163 : |
|
|
|
164 : |
|
|
PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path);
|
165 : |
|
|
|
166 : |
|
|
if (portletRequestDispatcher == null) {
|
167 : |
|
|
_log.error(path + " is not a valid include");
|
168 : |
|
|
} else {
|
169 : |
|
|
portletRequestDispatcher.include(renderRequest, renderResponse);
|
170 : |
|
|
}
|
171 : |
|
|
}
|
172 : |
|
|
|
173 : |
|
|
private double prepareUsageInformationList(String type) {
|
174 : |
|
|
// First we empty any list that can exists
|
175 : |
|
|
usageInformationList.clear();
|
176 : |
|
|
// We retrieve the usage records
|
177 : |
|
|
Set<SmartLMUsageRecordType> usageRecords = eventData.getUsageRecords();
|
178 : |
|
|
ArrayList<SmartLMUsageRecordTypeSorted> records = new ArrayList<SmartLMUsageRecordTypeSorted>();
|
179 : |
|
|
|
180 : |
|
|
for(SmartLMUsageRecordType usageRecord : usageRecords) {
|
181 : |
|
|
SmartLMUsageRecordTypeSorted record = new SmartLMUsageRecordTypeSorted(usageRecord);
|
182 : |
|
|
records.add(record);
|
183 : |
|
|
}
|
184 : |
|
|
|
185 : |
|
|
Collections.sort(records);
|
186 : |
|
|
|
187 : |
|
|
// We create the first line of information in the table
|
188 : |
|
|
List<String> line = new ArrayList<String>();
|
189 : |
|
|
line.add("User"); line.add("Product"); line.add("Start Time"); line.add("End Time"); line.add("AccountingGroup"); //line.add("Project"); //line.add("Host"); line.add("SubmitHost");
|
190 : |
|
|
if(type.equals(QueryPortlet.TYPE_BILLING)) line.add("Charge");
|
191 : |
|
|
usageInformationList.add(line);
|
192 : |
|
|
|
193 : |
|
|
double total = 0.0;
|
194 : |
|
|
for(SmartLMUsageRecordTypeSorted record: records) {
|
195 : |
|
|
SmartLMUsageRecordType usageRecord = record.getUsagerecord();
|
196 : |
|
|
String user = StringUtil.sortString(usageRecord.getUserIdentityArray(0).getLocalUserId());
|
197 : |
|
|
//String host = usageRecord.getHostArray(0).getStringValue();
|
198 : |
|
|
//String submitHost = usageRecord.getSubmitHostArray(0).getStringValue();
|
199 : |
|
|
//String project = usageRecord.getProjectNameArray(0).getStringValue();
|
200 : |
|
|
|
201 : |
|
|
LicenseResourceType[] licenseResources = usageRecord.getLicenseResourceArray();
|
202 : |
|
|
|
203 : |
|
|
for(int i = 0; i < licenseResources.length; i++) {
|
204 : |
|
|
LicenseResourceType licenseResource = licenseResources[i];
|
205 : |
|
|
String product = licenseResource.getLicense().getProduct().getName();
|
206 : |
|
|
String accountingGroup = StringUtil.sortString(licenseResource.getAccountingGroup());
|
207 : |
|
|
String start = licenseResource.getAccountingPeriod().getStartTime().toString();
|
208 : |
|
|
String end = licenseResource.getAccountingPeriod().getEndTime().toString();
|
209 : |
|
|
|
210 : |
|
|
List<String> newLine = new ArrayList<String>();
|
211 : |
|
|
newLine.add(user); newLine.add(product); newLine.add(start); newLine.add(end); newLine.add(accountingGroup);// newLine.add(project); newLine.add(host); newLine.add(submitHost);
|
212 : |
|
|
if(type.equals(QueryPortlet.TYPE_BILLING)) {
|
213 : |
|
|
double charge = Double.parseDouble(licenseResource.getCharge().getStringValue());
|
214 : |
|
|
if(charge < 0.0) charge = 0.0;
|
215 : |
|
|
total = total + charge;
|
216 : |
|
|
newLine.add(twoPlaces.format(charge)+"€");
|
217 : |
|
|
}
|
218 : |
|
|
usageInformationList.add(newLine);
|
219 : |
|
|
}
|
220 : |
|
|
}
|
221 : |
|
|
|
222 : |
|
|
return total;
|
223 : |
|
|
}
|
224 : |
|
|
}
|