Java tutorial
/*$Id: AbstractOrganisationBrowsingController.java 11232 2008-11-02 21:14:57Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2006 ABM-utvikling * * * * Created on 21-Feb-2006 * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.abmstatistikk.web; import javax.servlet.http.HttpServletRequest; import no.abmu.abmstatistikk.service.BrowseOrganisationRegisterService; import no.abmu.common.constants.ViewNameConst; import no.abmu.organisationregister.finders.organisationunit.OrgUnitFinderSpecificationBean; import no.abmu.organisationregister.service.OrganisationUnitService; import no.abmu.organisationregister.web.PageHolderOrganisationUnitImpl; import no.abmu.util.pageholder.PageHolder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.StopWatch; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.servlet.ModelAndView; /** * Abstract <code>MultiActionController</code> that is basis * for all organisationUnits browsing. * * @author Jens Vindvad, Jens.Vindvad@abm-utvikling.no * @author $Author: jens $ * @version $Rev: 11232 $ * @date $Date: 2008-11-02 22:14:57 +0100 (Sun, 02 Nov 2008) $ * @copyright ABM-Utvikling */ public abstract class AbstractOrganisationBrowsingController extends AbstractBaseController { private static final Log logger = (Log) LogFactory.getLog(AbstractOrganisationBrowsingController.class); private OrganisationUnitService organisationUnitService; private BrowseOrganisationRegisterService browseOrganisationRegisterService; public void setBrowseOrganisationRegisterService( BrowseOrganisationRegisterService browseOrganisationRegisterService) { this.browseOrganisationRegisterService = browseOrganisationRegisterService; } public void setOrganisationUnitService(OrganisationUnitService organisationUnitService) { this.organisationUnitService = organisationUnitService; } /** * Browse sets up and handles pageHolder browsing of organisationUnits. * * @param request current HTTP request * @param attribute attribute in current HttpSession * @param finderBean specifies which organisationUnits to browse * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @return ModelAndView holder for both Model and View in * the web MVC framework to be resolved by a DispatcherServlet. */ protected ModelAndView browseFinderSpesification(HttpServletRequest request, String attribute, OrgUnitFinderSpecificationBean finderBean, String viewName) { /* * This should be replaced with AOP in an XML spring config-file. */ if (!isSecureContext()) { return new ModelAndView(ViewNameConst.LOGOFF_VIEW); } return browse(request, attribute, finderBean, viewName); } /** * Browse by county. * * @param request current HTTP request * @param attribute in current HttpSession * @param organisationTypeName name on organisationType to browse * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @return ModelAndView holder for both Model and View in * the web MVC framework to be resolved by a DispatcherServlet. */ protected ModelAndView browseOrgTypeByCounty(HttpServletRequest request, String attribute, String organisationTypeName, String viewName) { /* * This should be replaced with AOP in an XML spring config-file. */ if (!isSecureContext()) { logger.error("[browseOrgTypeByCounty]: no securityService, logg off."); return new ModelAndView(ViewNameConst.LOGOFF_VIEW); } StopWatch stopWatch = new StopWatch("browseOrgType"); stopWatch.start("Mav_for_browsing_organisationType_by_county"); Integer countynumber = getCountyNumberFromRequestOrLoggedOnUser(request); if (null == countynumber || countynumber.intValue() == 0) { logger.error("[browseOrgTypeByCounty]: no countynumber"); return new ModelAndView(ViewNameConst.REDIRECT_2_MAIN_MENU); } OrgUnitFinderSpecificationBean finderBean = new OrgUnitFinderSpecificationBean(); finderBean.setOrganisationTypeName(organisationTypeName); finderBean.setCountyNumber(countynumber); finderBean.setTodayAsActiveDate(); ModelAndView mav = browse(request, attribute, finderBean, viewName); stopWatch.stop(); logger.info("Getting ModelAndview for organisationType " + organisationTypeName + " for county " + countynumber + " tok " + stopWatch.getTotalTimeMillis() + " milliseconds."); return mav; } /** * Browse an organisationUnit together with its children. * * @param request current HTTP request * @param attribute in current HttpSession * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @return ModelAndView holder for both Model and View in * the web MVC framework to be resolved by a DispatcherServlet. */ protected ModelAndView browseChildren(HttpServletRequest request, String attribute, String viewName) { /* * This should be replaced with AOP in an XML spring config-file. */ if (!isSecureContext()) { return new ModelAndView(ViewNameConst.LOGOFF_VIEW); } Long orgUnitId = getWorkingSchemaOrgUnitId(request); if (orgUnitId == null) { return new ModelAndView(ViewNameConst.REDIRECT_2_MAIN_MENU); } return browseChildren(request, attribute, orgUnitId, viewName); } // //////////////////////////////////////////////////////////////////////// // // Private methods for organisation browsing // // //////////////////////////////////////////////////////////////////////// /** * Browse sets up and handles pageHolder browsing of organisationUnits. * * @param request current HTTP request * @param attribute in current HttpSession * @param finderBean specifies which organisationUnits to browse * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @return ModelAndView holder for both Model and View in * the web MVC framework to be resolved by a DispatcherServlet. */ private ModelAndView browse(HttpServletRequest request, String attribute, OrgUnitFinderSpecificationBean finderBean, String viewName) { PageHolder pageHolder = (PageHolder) request.getSession(true).getAttribute(attribute); if (null == pageHolder) { pageHolder = browseOrganisationRegisterService.createNewPageHolder(finderBean); if (viewName.equals("kkdBrowseReportingMuseumView")) { pageHolder.setCurrentPageSize(pageHolder.getNrOfElements()); pageHolder.getSort().setProperty("countyNumberAndOrganisationUnitName"); } request.getSession().setAttribute(attribute, pageHolder); } ServletRequestDataBinder binder = new ServletRequestDataBinder(pageHolder, "pageHolder"); binder.bind(request); pageHolder.reload(false); return new ModelAndView(viewName, binder.getBindingResult().getModel()); } /** * Browse children. * * @param request current HTTP request * @param attribute in current HttpSession * @param parentOrganisationUnitId * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @return ModelAndView holder for both Model and View in * the web MVC framework to be resolved by a DispatcherServlet. */ private ModelAndView browseChildren(HttpServletRequest request, String attribute, Long parentOrganisationUnitId, String viewName) { PageHolderOrganisationUnitImpl pageHolder = (PageHolderOrganisationUnitImpl) request.getSession(true) .getAttribute(attribute); if (null == pageHolder || parentOrganisationUnitId != pageHolder.getWorkingFromOrgUnitId()) { pageHolder = browseOrganisationRegisterService .createNewPageHolderWithChildren(parentOrganisationUnitId); request.getSession().setAttribute(attribute, pageHolder); } ServletRequestDataBinder binder = new ServletRequestDataBinder(pageHolder, "pageHolder"); binder.bind(request); pageHolder.reload(false); return new ModelAndView(viewName, binder.getBindingResult().getModel()); } // //////////////////////////////////////////////////////////////////////// // // Private class // // //////////////////////////////////////////////////////////////////////// /** * ddd. * */ /* private class PageHolderDaoImpl implements PageHolderDao { private OrgUnitStatusReportFinderSpecification finder; private Integer numberOfRecord; public PageHolderDaoImpl(OrgUnitFinderSpecificationBean finderBean) { this.finder = new OrgUnitStatusReportFinderSpecification(finderBean); } public List getPageList(String queryString, int firstElement, int pageSize) { String orderBy = queryString.trim(); logger.info("Query string from PageHolder [" + queryString + "]"); StopWatch stopWatch = new StopWatch("getPageList"); stopWatch.start("getPageList"); finder.setFirstElement(firstElement); finder.setPageSize(pageSize); if (orderBy.equals("order by name") || orderBy.equals("order by name asc")) { logger.info("Setting order to [ order by orgUnit.municipalityNumber asc]"); finder.setOrderBy(" order by orgUnit.name.reference asc"); } else if (orderBy.equals("order by name desc")) { logger.info("Setting order to [ order by orgUnit.municipalityNumber desc]"); finder.setOrderBy(" order by orgUnit.name.reference desc"); } //getByDefaultLocale //finder.setOrderBy(" order by orgUnit.getId()"); logger.info("QueryString: " + finder.getQueryString()); logger.info(finder.getOrgUnitFinderSpecificationBean().toString()); List resultList = (List) organisationUnitService.find(finder); stopWatch.stop(); logger.info("QueryString: " + finder.getQueryString()); logger.info("Getting list of " + resultList.size() + " elements " + " tok " + stopWatch.getTotalTimeMillis() + " milliseconds."); return resultList; } public int getNrOfElements(String countString) { //String realCountString = this.finder.getCountString(); //logger.info("CountString: " + realCountString); if (null == numberOfRecord) { numberOfRecord = organisationUnitService.findCount(finder); } //Integer nrOfElements = organisationUnitService.findCount(finder); //logger.info("Number of elements: " + nrOfElements); return numberOfRecord; } } */ /** * dddd. * */ /* private class SourceProviderImpl implements SourceProvider { private OrgUnitFinderSpecificationBean finderBean; public SourceProviderImpl(OrgUnitFinderSpecificationBean finderBean) { this.finderBean = finderBean; } public PageHolderSource loadSource(boolean force, Locale loc, Object filter) { PageHolderDao pageHolderDao = new PageHolderDaoImpl(finderBean); return new PageHolderSourceImpl(pageHolderDao, "", ""); } } */ }