/**
* Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package eu.smartlm.abs.portal.view.portlet;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Event;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import eu.smartlm.abs.portal.security.login.controller.LoginController;
import eu.smartlm.abs.portal.util.StringUtil;
import eu.smartlm.abs.portal.view.event.EventData;
import eu.smartlm.abs.portal.view.portlet.model.SmartLMUsageRecordTypeSorted;
import eu.smartlm.schemas.x2009.x06.urec.LicenseResourceType;
import eu.smartlm.schemas.x2009.x06.urec.SmartLMUsageRecordType;
/**
* Shows the results of a query to the accounting and billing system. Both in graphs done by
* JFreeChart and in a informative table.
*
* @author David Garcia Perez-CESGA
*
*/
public class ResultPortlet extends GenericPortlet {
private String type;
public static String GRAPH_DONE = "graphDone";
public static String GRAPH_DONE_BILLING = "graphDoneBilling";
public static String GRAPH = "graph";
public List<List<String>> usageInformationList = new ArrayList<List<String>>();
protected String editJSP;
protected String helpJSP;
protected String viewJSP;
private static Log _log = LogFactoryUtil.getLog(ResultPortlet.class);
private DecimalFormat twoPlaces = new DecimalFormat("0.00");
private EventData eventData = new EventData(false, null, new HashSet<SmartLMUsageRecordType>(), type, "", "");
@Override
public void init() throws PortletException {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
type = getInitParameter("type");
}
@Override
public void doDispatch(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
String jspPage = renderRequest.getParameter("jspPage");
if (jspPage != null) {
include(jspPage, renderRequest, renderResponse);
} else {
super.doDispatch(renderRequest, renderResponse);
}
}
@Override
public void doEdit(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
if (renderRequest.getPreferences() == null) {
super.doEdit(renderRequest, renderResponse);
} else {
include(editJSP, renderRequest, renderResponse);
}
}
@Override
public void doHelp(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
include(helpJSP, renderRequest, renderResponse);
}
@Override
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
PortletSession session = renderRequest.getPortletSession();
// We show information if the user is logged in.
int status = LoginController.LOGGED_OUT;
Integer loginStatus = (Integer) session.getAttribute(LoginController.LOGIN_EVENT, PortletSession.APPLICATION_SCOPE);
if(loginStatus != null) status = loginStatus.intValue();
session.setAttribute("usageInformationList", usageInformationList);
if(eventData.isRepresent() && eventData.getGraph() != null && status == LoginController.LOGGED_IN) {
session.setAttribute("graph", eventData.getGraph(), PortletSession.APPLICATION_SCOPE);
session.setAttribute("eventpdf", eventData, PortletSession.APPLICATION_SCOPE);
session.setAttribute("representGraph", true);
session.setAttribute("eventdata", eventData);
}
else
session.setAttribute("representGraph", false);
include(viewJSP, renderRequest, renderResponse);
}
@Override
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
}
@Override
public void processEvent(EventRequest eventRequest, EventResponse eventResponse) {
Event event = eventRequest.getEvent();
// We check if we need to represent the Graph.
if(type.equals(QueryPortlet.TYPE_ACCOUNTING)) {
if(event.getName().equals(ResultPortlet.GRAPH_DONE)) {
eventData = (EventData) event.getValue();
prepareUsageInformationList(type);
}
}
else if (type.equals(QueryPortlet.TYPE_BILLING)) {
if(event.getName().equals(ResultPortlet.GRAPH_DONE_BILLING)) {
eventData = (EventData) event.getValue();
double total = prepareUsageInformationList(type);
String totalString = twoPlaces.format(total);
eventData.setTotal(totalString);
}
}
}
protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
PortletRequestDispatcher portletRequestDispatcher = getPortletContext().getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
} else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
private double prepareUsageInformationList(String type) {
// First we empty any list that can exists
usageInformationList.clear();
// We retrieve the usage records
Set<SmartLMUsageRecordType> usageRecords = eventData.getUsageRecords();
ArrayList<SmartLMUsageRecordTypeSorted> records = new ArrayList<SmartLMUsageRecordTypeSorted>();
for(SmartLMUsageRecordType usageRecord : usageRecords) {
SmartLMUsageRecordTypeSorted record = new SmartLMUsageRecordTypeSorted(usageRecord);
records.add(record);
}
Collections.sort(records);
// We create the first line of information in the table
List<String> line = new ArrayList<String>();
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");
if(type.equals(QueryPortlet.TYPE_BILLING)) line.add("Charge");
usageInformationList.add(line);
double total = 0.0;
for(SmartLMUsageRecordTypeSorted record: records) {
SmartLMUsageRecordType usageRecord = record.getUsagerecord();
String user = StringUtil.sortString(usageRecord.getUserIdentityArray(0).getLocalUserId());
//String host = usageRecord.getHostArray(0).getStringValue();
//String submitHost = usageRecord.getSubmitHostArray(0).getStringValue();
//String project = usageRecord.getProjectNameArray(0).getStringValue();
LicenseResourceType[] licenseResources = usageRecord.getLicenseResourceArray();
for(int i = 0; i < licenseResources.length; i++) {
LicenseResourceType licenseResource = licenseResources[i];
String product = licenseResource.getLicense().getProduct().getName();
String accountingGroup = StringUtil.sortString(licenseResource.getAccountingGroup());
String start = licenseResource.getAccountingPeriod().getStartTime().toString();
String end = licenseResource.getAccountingPeriod().getEndTime().toString();
List<String> newLine = new ArrayList<String>();
newLine.add(user); newLine.add(product); newLine.add(start); newLine.add(end); newLine.add(accountingGroup);// newLine.add(project); newLine.add(host); newLine.add(submitHost);
if(type.equals(QueryPortlet.TYPE_BILLING)) {
double charge = Double.parseDouble(licenseResource.getCharge().getStringValue());
if(charge < 0.0) charge = 0.0;
total = total + charge;
newLine.add(twoPlaces.format(charge)+"€");
}
usageInformationList.add(newLine);
}
}
return total;
}
}