001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.console.derbylogmanager;
019    
020    import org.apache.geronimo.console.BasePortlet;
021    import org.apache.geronimo.console.util.PortletManager;
022    import org.apache.geronimo.derby.DerbyLog;
023    
024    import javax.portlet.ActionRequest;
025    import javax.portlet.ActionResponse;
026    import javax.portlet.PortletConfig;
027    import javax.portlet.PortletContext;
028    import javax.portlet.PortletException;
029    import javax.portlet.PortletRequestDispatcher;
030    import javax.portlet.PortletSession;
031    import javax.portlet.RenderRequest;
032    import javax.portlet.RenderResponse;
033    import javax.portlet.WindowState;
034    import java.io.IOException;
035    import java.io.Serializable;
036    
037    public class DerbyLogViewerPortlet extends BasePortlet {
038        private final static String CRITERIA_KEY = "org.apache.geronimo.console.derby.log.CRITERIA";
039    
040        protected PortletRequestDispatcher normalView;
041    
042        protected PortletRequestDispatcher helpView;
043    
044        public void destroy() {
045            super.destroy();
046            normalView = null;
047            helpView = null;
048        }
049    
050        protected void doHelp(RenderRequest renderRequest,
051                              RenderResponse renderResponse) throws PortletException, IOException {
052            helpView.include(renderRequest, renderResponse);
053        }
054    
055        protected void doView(RenderRequest renderRequest,
056                              RenderResponse renderResponse) throws PortletException, IOException {
057            if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
058                return;
059            }
060            String action = renderRequest.getParameter("action");
061    
062            DerbyLog log = (DerbyLog) PortletManager.getManagedBeans(renderRequest, DerbyLog.class)[0];//todo: what if it's not there?
063            Criteria criteria = (Criteria) renderRequest.getPortletSession(true).getAttribute(CRITERIA_KEY, PortletSession.PORTLET_SCOPE);
064            
065            if (criteria == null || (action != null && !"refresh".equals(action))) {
066                if(criteria == null)
067                    criteria = new Criteria();
068                String startPos = renderRequest.getParameter("startPos");
069                String endPos = renderRequest.getParameter("endPos");
070                String maxRows = renderRequest.getParameter("maxRows");
071                String searchString = renderRequest.getParameter("searchString");           
072                
073                criteria.max = maxRows == null || maxRows.equals("") ? criteria.max : Integer.parseInt(maxRows);
074                criteria.start = startPos == null || startPos.equals("") ? null : new Integer(startPos);
075                criteria.stop = endPos == null || endPos.equals("") ? null : new Integer(endPos);
076                criteria.text = searchString == null || searchString.equals("") ? null : searchString;
077                renderRequest.getPortletSession(true).setAttribute(CRITERIA_KEY, criteria, PortletSession.PORTLET_SCOPE);
078            }
079    
080            DerbyLog.SearchResults results = log.searchLog(criteria.start, criteria.stop,
081                             criteria.max, criteria.text);
082            renderRequest.setAttribute("searchResults", results.getResults());
083            renderRequest.setAttribute("lineCount", new Integer(results.getLineCount()));
084            renderRequest.setAttribute("startPos", criteria.start);
085            renderRequest.setAttribute("endPos", criteria.stop);
086            renderRequest.setAttribute("searchString", criteria.text);
087            renderRequest.setAttribute("maxRows", criteria.max);
088            if(results.isCapped()) {
089                renderRequest.setAttribute("capped", Boolean.TRUE);
090            }
091    
092            normalView.include(renderRequest, renderResponse);
093        }
094    
095        @Override
096        public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException {        
097            //Add all the parameters to the actionResponse Attributes so we can get the back
098            actionResponse.setRenderParameters(actionRequest.getParameterMap());    
099        }
100        
101        private static class Criteria implements Serializable {
102            Integer max = 10;
103            Integer start;
104            Integer stop;
105            String text;
106        }
107    
108        public void init(PortletConfig portletConfig) throws PortletException {
109            PortletContext pc = portletConfig.getPortletContext();
110            normalView = pc
111                    .getRequestDispatcher("/WEB-INF/view/derbylogmanager/view.jsp");
112            helpView = pc
113                    .getRequestDispatcher("/WEB-INF/view/derbylogmanager/help.jsp");
114            super.init(portletConfig);
115        }
116    }