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.PortletConfig;
025    import javax.portlet.PortletContext;
026    import javax.portlet.PortletException;
027    import javax.portlet.PortletRequestDispatcher;
028    import javax.portlet.PortletSession;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    import javax.portlet.WindowState;
032    import java.io.IOException;
033    import java.io.Serializable;
034    
035    public class DerbyLogViewerPortlet extends BasePortlet {
036        private final static String CRITERIA_KEY = "org.apache.geronimo.console.derby.log.CRITERIA";
037    
038        protected PortletRequestDispatcher normalView;
039    
040        protected PortletRequestDispatcher helpView;
041    
042        public void destroy() {
043            super.destroy();
044            normalView = null;
045            helpView = null;
046        }
047    
048        protected void doHelp(RenderRequest renderRequest,
049                              RenderResponse renderResponse) throws PortletException, IOException {
050            helpView.include(renderRequest, renderResponse);
051        }
052    
053        protected void doView(RenderRequest renderRequest,
054                              RenderResponse renderResponse) throws PortletException, IOException {
055            if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
056                return;
057            }
058            String action = renderRequest.getParameter("action");
059    
060            DerbyLog log = (DerbyLog) PortletManager.getManagedBeans(renderRequest, DerbyLog.class)[0];//todo: what if it's not there?
061            Criteria criteria = (Criteria) renderRequest.getPortletSession(true).getAttribute(CRITERIA_KEY, PortletSession.PORTLET_SCOPE);
062            
063            if (criteria == null || (action != null && !"refresh".equals(action))) {
064                if(criteria == null)
065                    criteria = new Criteria();
066                String startPos = renderRequest.getParameter("startPos");
067                String endPos = renderRequest.getParameter("endPos");
068                String maxRows = renderRequest.getParameter("maxRows");
069                String searchString = renderRequest.getParameter("searchString");           
070                
071                criteria.max = maxRows == null || maxRows.equals("") ? criteria.max : Integer.parseInt(maxRows);
072                criteria.start = startPos == null || startPos.equals("") ? null : new Integer(startPos);
073                criteria.stop = endPos == null || endPos.equals("") ? null : new Integer(endPos);
074                criteria.text = searchString == null || searchString.equals("") ? null : searchString;
075                renderRequest.getPortletSession(true).setAttribute(CRITERIA_KEY, criteria, PortletSession.PORTLET_SCOPE);
076            }
077    
078            DerbyLog.SearchResults results = log.searchLog(criteria.start, criteria.stop,
079                             criteria.max, criteria.text);
080            renderRequest.setAttribute("searchResults", results.getResults());
081            renderRequest.setAttribute("lineCount", new Integer(results.getLineCount()));
082            renderRequest.setAttribute("startPos", criteria.start);
083            renderRequest.setAttribute("endPos", criteria.stop);
084            renderRequest.setAttribute("searchString", criteria.text);
085            renderRequest.setAttribute("maxRows", criteria.max);
086            if(results.isCapped()) {
087                renderRequest.setAttribute("capped", Boolean.TRUE);
088            }
089    
090            normalView.include(renderRequest, renderResponse);
091        }
092    
093        private static class Criteria implements Serializable {
094            Integer max = 10;
095            Integer start;
096            Integer stop;
097            String text;
098        }
099    
100        public void init(PortletConfig portletConfig) throws PortletException {
101            PortletContext pc = portletConfig.getPortletContext();
102            normalView = pc
103                    .getRequestDispatcher("/WEB-INF/view/derbylogmanager/view.jsp");
104            helpView = pc
105                    .getRequestDispatcher("/WEB-INF/view/derbylogmanager/help.jsp");
106            super.init(portletConfig);
107        }
108    }