package eu.smartlm.abs.portal.view.portlet; import java.io.File; import java.io.IOException; import java.util.HashSet; 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 org.jfree.chart.JFreeChart; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import eu.smartlm.abs.portal.data.query.QueryConstructor; import eu.smartlm.abs.portal.data.query.QueryConstructorDummy; import eu.smartlm.abs.portal.data.query.ws.EBSServiceClient; import eu.smartlm.abs.portal.graph.GraphConstructor; import eu.smartlm.abs.portal.security.login.controller.LoginController; import eu.smartlm.abs.portal.security.login.model.User; import eu.smartlm.abs.portal.util.StringUtil; import eu.smartlm.abs.portal.view.data.ABSQuery; import eu.smartlm.abs.portal.view.event.EventData; import eu.smartlm.abs.portal.view.event.LoginEventData; import eu.smartlm.schemas.x2009.x06.urec.SmartLMUsageRecordType; /** * Query constructor for the Accounting and Billing Portal * * @author David García Pérez-CESGA * */ public class QueryPortlet extends GenericPortlet { public static String TYPE_ACCOUNTING = "100"; public static String TYPE_BILLING = "200"; private String type; public static String QUERY = "query"; private ABSQuery aBSquery = new ABSQuery(); private boolean creation; private String ebsInterfaceURL; public static String EBS_INTERFACE_URL = "ebs_interface_url"; private Set securityUserFilter = new HashSet(); private Set securityAccountingGroupFilter = new HashSet(); protected Set getSecurityUserFilter() { return securityUserFilter; } protected Set getSecurityAccountingGroupFilter() { return securityAccountingGroupFilter; } @Override public void init() throws PortletException { type = getInitParameter("type"); ebsInterfaceURL = getInitParameter(EBS_INTERFACE_URL); editJSP = getInitParameter("edit-jsp"); helpJSP = getInitParameter("help-jsp"); viewJSP = getInitParameter("view-jsp"); creation = true; aBSquery.setLoginStatus(LoginController.LOGGED_OUT); } @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(true); aBSquery.setSecurityUserFilter(securityUserFilter); aBSquery.setSecurityAccountingGroupFilter(securityAccountingGroupFilter); if (creation) { aBSquery.reset(); creation = false; } Integer status = (Integer) session.getAttribute(LoginController.LOGIN_EVENT, PortletSession.APPLICATION_SCOPE); if(status != null) { System.out.println("CREATING VIEW ################################"); setLoginStatus(status.intValue(), (User) session.getAttribute(LoginController.LOGIN_EVENT_USER, PortletSession.APPLICATION_SCOPE)); } session.setAttribute(QUERY, aBSquery); session.setAttribute(EBS_INTERFACE_URL, ebsInterfaceURL); include(viewJSP, renderRequest, renderResponse); } @Override public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String selection = actionRequest.getParameter("selection"); String[] selectedOptions = actionRequest.getParameterValues("optionsFromSelection"); String startDate = actionRequest.getParameter("startDate"); String endDate = actionRequest.getParameter("endDate"); if (startDate != null && startDate != "") { aBSquery.setStartDate(startDate); } if (endDate != null & endDate != "") { aBSquery.setEndDate(endDate); } if (actionRequest.getParameter("moreOptions") != null) { //We take different actions depending on the status if (selection != null && selectedOptions != null) { addToQuery(selection, selectedOptions); actAsStatus(selection); } } else if (actionRequest.getParameter("Submit") != null) { if (selection != null && selectedOptions != null) { addToQuery(selection, selectedOptions); actAsStatus(selection); } // We get the Usage Records QueryConstructor queryConstructor; if (ebsInterfaceURL.equalsIgnoreCase("DEMO")) { queryConstructor = new QueryConstructorDummy(); } else { queryConstructor = new EBSServiceClient(); try { File rampartDirectory = new File("rampart"); File clientPropertiesFile = new File("client.properties"); queryConstructor.setUpConnection(ebsInterfaceURL, rampartDirectory.getCanonicalPath(), clientPropertiesFile.getCanonicalPath()); } catch (Exception e) { throw new PortletException("Unable to initialize ConfigurationContext.", e); } } // We establish the security restrinctions inside the query Set users = new HashSet(); for(String user : aBSquery.getQueryUsers()) users.add(user); for(String filterUser : securityUserFilter) users.add(filterUser); Set accountingGroups = new HashSet(); for(String accountingGroup : aBSquery.getQueryAccountingGroups()) accountingGroups.add(accountingGroup); for(String filterAccountingGroup : securityAccountingGroupFilter) accountingGroups.add(filterAccountingGroup); Set usageRecords = queryConstructor.getListOfSmartLMUsageRecords( StringUtil.getDate(aBSquery.getStartDate(), false), StringUtil.getDate(aBSquery.getEndDate(), true), users, aBSquery.getQueryProducts(), aBSquery.getQueryProjects(), aBSquery.getQueryHosts(), aBSquery.getQuerySubmitHosts(), new HashSet(), accountingGroups); // We construct the graph JFreeChart graph = null; if (type.equals(TYPE_ACCOUNTING)) { graph = GraphConstructor.constructHourGraph(aBSquery, GraphConstructor.GRAPH_TYPE_BARCHAR3D, usageRecords); } else if (type.equals(TYPE_BILLING)) { graph = GraphConstructor.constructBillingGraph(aBSquery, GraphConstructor.GRAPH_TYPE_BARCHAR3D, usageRecords); } EventData eventData = new EventData(true, graph, usageRecords, type, aBSquery.getStartDate(), aBSquery.getEndDate()); eventData.setQueryUsers(StringUtil.copyStringList(aBSquery.getQueryUsers())); eventData.setQueryProducts(StringUtil.copyStringList(aBSquery.getQueryProducts())); eventData.setQueryProjects(StringUtil.copyStringList(aBSquery.getQueryProjects())); eventData.setQueryHosts(StringUtil.copyStringList(aBSquery.getQueryHosts())); eventData.setQuerySubmitHosts(StringUtil.copyStringList(aBSquery.getQuerySubmitHosts())); eventData.setQueryAccountingGroups(StringUtil.copyStringList(aBSquery.getQueryAccountingGroups())); // We reset the query at the end. aBSquery.reset(); // We send an event that a Graph needs to be represented. if (type.equals(TYPE_ACCOUNTING)) { actionResponse.setEvent(ResultPortlet.GRAPH_DONE, eventData); } else if (type.equals(TYPE_BILLING)) { actionResponse.setEvent(ResultPortlet.GRAPH_DONE_BILLING, eventData); } } else if (actionRequest.getParameter("Reset") != null) { aBSquery.reset(); EventData eventData = new EventData(false, null, new HashSet(), type, "", ""); //actionResponse.setEvent(ResultPortlet.GRAPH_DONE, eventData); // We send an event that a Graph needs to be represented. if (type.equals(TYPE_ACCOUNTING)) { actionResponse.setEvent(ResultPortlet.GRAPH_DONE, eventData); } else if (type.equals(TYPE_BILLING)) { actionResponse.setEvent(ResultPortlet.GRAPH_DONE_BILLING, eventData); } } } @Override public void processEvent(EventRequest eventRequest, EventResponse eventResponse) { System.out.println("GETS EVENT - " + type); System.out.println("PROCESS EVENT #####################"); Event event = eventRequest.getEvent(); if(event.getName().equals(LoginController.LOGIN_EVENT)) { LoginEventData loginEventData = (LoginEventData) event.getValue(); setLoginStatus(loginEventData.getStatus(), loginEventData.getUser()); } } private void printStatus(int status) { if(type.equals(TYPE_ACCOUNTING)) { if(status == LoginController.LOGGED_IN) System.out.println("ACCOUNTING PORTLET - USER LOGGED IN"); else System.out.println("ACCOUNTING PORTLET - USER LOGGED OUT"); } else { if(status == LoginController.LOGGED_IN) System.out.println("BILLING PORTLET - USER LOGGED IN"); else System.out.println("BILLING PORTLET - USER LOGGED OUT"); } } private void setLoginStatus(int status, User user) { printStatus(status); aBSquery.setLoginStatus(status); if(aBSquery.getLoginStatus() == LoginController.LOGGED_OUT) { aBSquery.reset(); securityAccountingGroupFilter = new HashSet(); securityUserFilter = new HashSet(); aBSquery.setSecurityAccountingGroupFilter(securityAccountingGroupFilter); aBSquery.setSecurityUserFilter(securityUserFilter); } else if (aBSquery.getLoginStatus() == LoginController.LOGGED_IN) { setFilters(user); aBSquery.setSecurityAccountingGroupFilter(securityAccountingGroupFilter); aBSquery.setSecurityUserFilter(securityUserFilter); } } private void addToQuery(String selection, String[] selectedOptions) { Set selectedOptionsList = new HashSet(); //We convert the String[] to a List for (int i = 0; i < selectedOptions.length; i++) { selectedOptionsList.add(selectedOptions[i]); } if (selection.equals(ABSQuery.PRODUCT)) { aBSquery.setQueryProducts(selectedOptionsList); } else if (selection.equals(ABSQuery.USER)) { aBSquery.setQueryUsers(selectedOptionsList); } else if (selection.equals(ABSQuery.ACCOUNTINGGROUP)) { aBSquery.setQueryAccountingGroups(selectedOptionsList); } else if (selection.equals(ABSQuery.PROJECT)) { aBSquery.setQueryProjects(selectedOptionsList); } else if (selection.equals(ABSQuery.HOST)) { aBSquery.setQueryHosts(selectedOptionsList); } else if (selection.equals(ABSQuery.SUBMITHOST)) { aBSquery.setQuerySubmitHosts(selectedOptionsList); } } private void actAsStatus(String parameterSelection) { int numberOfOptions = aBSquery.getNumberOptions(); // We decrease the number of options. if (!parameterSelection.equals("Select Option")) { aBSquery.setNumberOptions(numberOfOptions - 1); } if (parameterSelection.equals(ABSQuery.PRODUCT)) { aBSquery.setProducts(false); if (numberOfOptions == ABSQuery.NUMBER_OF_OPTIONS) { aBSquery.setFirstOption(ABSQuery.PRODUCT); } else if (numberOfOptions == (ABSQuery.NUMBER_OF_OPTIONS - 1)) { aBSquery.setSecondOption(ABSQuery.PRODUCT); } } else if (parameterSelection.equals(ABSQuery.USER)) { aBSquery.setUsers(false); if (numberOfOptions == ABSQuery.NUMBER_OF_OPTIONS) { aBSquery.setFirstOption(ABSQuery.USER); } else if (numberOfOptions == (ABSQuery.NUMBER_OF_OPTIONS - 1)) { aBSquery.setSecondOption(ABSQuery.USER); } } else if (parameterSelection.equals(ABSQuery.ACCOUNTINGGROUP)) { aBSquery.setAccountingGroups(false); if (numberOfOptions == ABSQuery.NUMBER_OF_OPTIONS) { aBSquery.setFirstOption(ABSQuery.ACCOUNTINGGROUP); } else if (numberOfOptions == (ABSQuery.NUMBER_OF_OPTIONS - 1)) { aBSquery.setSecondOption(ABSQuery.ACCOUNTINGGROUP); } } else if (parameterSelection.equals(ABSQuery.PROJECT)) { aBSquery.setProjects(false); if (numberOfOptions == ABSQuery.NUMBER_OF_OPTIONS) { aBSquery.setFirstOption(ABSQuery.PROJECT); } else if (numberOfOptions == (ABSQuery.NUMBER_OF_OPTIONS - 1)) { aBSquery.setSecondOption(ABSQuery.PROJECT); } } else if (parameterSelection.equals(ABSQuery.HOST)) { aBSquery.setHosts(false); if (numberOfOptions == ABSQuery.NUMBER_OF_OPTIONS) { aBSquery.setFirstOption(ABSQuery.HOST); } else if (numberOfOptions == (ABSQuery.NUMBER_OF_OPTIONS - 1)) { aBSquery.setSecondOption(ABSQuery.HOST); } } else if (parameterSelection.equals(ABSQuery.SUBMITHOST)) { aBSquery.setSubmithosts(false); if (numberOfOptions == ABSQuery.NUMBER_OF_OPTIONS) { aBSquery.setFirstOption(ABSQuery.SUBMITHOST); } else if (numberOfOptions == (ABSQuery.NUMBER_OF_OPTIONS - 1)) { aBSquery.setSecondOption(ABSQuery.SUBMITHOST); } } } 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); } } protected String editJSP; protected String helpJSP; protected String viewJSP; private static Log _log = LogFactoryUtil.getLog(QueryPortlet.class); /** * Sets the filter that are going to be added to all queries... * @param user */ public void setFilters(User user) { // We empty first the previous filters securityUserFilter = new HashSet(); securityAccountingGroupFilter = new HashSet(); if(user.isSeeAccountingGroupData() && !user.isSeeAll()) { securityAccountingGroupFilter.add(user.getAccountingGroup()); } else if(!user.isSeeAll()){ securityUserFilter.add(user.getAccountingUsername()); } } }