showcalendaraction.java

来自「开源项目CRM之OpenCustomer」· Java 代码 · 共 192 行

JAVA
192
字号
/*******************************************************************************
 * ***** BEGIN LICENSE BLOCK Version: MPL 1.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * 
 * The Original Code is the OpenCustomer CRM.
 * 
 * The Initial Developer of the Original Code is Thomas Bader (Bader & Jene
 * Software-Ingenieurb黵o). Portions created by the Initial Developer are
 * Copyright (C) 2005 the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s): Thomas Bader <thomas.bader@bader-jene.de>
 * 
 * ***** END LICENSE BLOCK *****
 */

package org.opencustomer.application.web.module.calendar;

import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.HibernateException;
import org.opencustomer.application.db.dao.calendar.CalendarDAO;
import org.opencustomer.application.db.vo.calendar.CalendarVO;
import org.opencustomer.application.db.vo.system.UserVO;
import org.opencustomer.application.web.Globals;
import org.opencustomer.application.web.module.calendar.view.CalendarView;
import org.opencustomer.application.web.struts.Action;
import org.opencustomer.db.EntityAccessUtility;
import org.opencustomer.db.vo.EntityAccess;

public class ShowCalendarAction extends Action<ShowCalendarForm>
{
    private static Logger log = Logger.getLogger(ShowCalendarAction.class);

    public ActionForward execute(ActionMapping mapping, ShowCalendarForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        boolean update = true;

        // Pr黤e ob schon ein Standardkalender besteht
        UserVO user = (UserVO) request.getSession().getAttribute(Globals.USER_KEY);
        CalendarVO calendar = (CalendarVO) request.getSession().getAttribute(Constants.CALENDAR_KEY);

        if (form.getId() > 0)
        {
            calendar = new CalendarDAO().getById(form.getId());
            if (!EntityAccessUtility.isAccessGranted(user, calendar, EntityAccess.Access.READ))
            {
                log.error("user tries to access an invalid calendar");
                calendar = null;
            }
            else
            {
                if (log.isDebugEnabled())
                    log.debug("use calendar: " + calendar);
            }
        }
        else if (form.isMain() || calendar == null)
        {
            // get main calendar if nothing available
            calendar = getMainCalendar(user);
            if (log.isDebugEnabled())
                log.debug("use main calendar: " + calendar);
        }

        // update calendar
        if (!calendar.equals(request.getSession().getAttribute(Constants.CALENDAR_KEY)))
        {
            if (log.isDebugEnabled())
                log.debug("update calendar");
            request.getSession().removeAttribute(Constants.CALENDAR_KEY);
            request.getSession().removeAttribute(Constants.CALENDAR_ViEW_KEY);
        }

        request.getSession().setAttribute(Constants.CALENDAR_KEY, calendar);

        CalendarView calendarView = (CalendarView) request.getSession().getAttribute(Constants.CALENDAR_ViEW_KEY);
        if (calendarView == null)
        {
            Date referenceDate = new Date();
            if (request.getSession().getAttribute(Constants.REFERENCE_DAY_KEY) != null)
                referenceDate = (Date) request.getSession().getAttribute(Constants.REFERENCE_DAY_KEY);

            calendarView = new CalendarView(calendar.getId(), referenceDate, java.util.Calendar.MONDAY, CalendarView.Period.MONTH, user);
            request.getSession().setAttribute(Constants.CALENDAR_ViEW_KEY, calendarView);
            update = false;
        }

        if (form.isDoLastMonth())
        {
            if (log.isDebugEnabled())
                log.debug("go to last month");

            java.util.Calendar cal = GregorianCalendar.getInstance();
            cal.setTime(calendarView.getReferenceDate());
            if (cal.get(java.util.Calendar.MONTH) == java.util.Calendar.JANUARY)
                cal.roll(java.util.Calendar.YEAR, -1);
            cal.roll(java.util.Calendar.MONTH, -1);
            calendarView.setReferenceDate(cal.getTime());

            request.getSession().setAttribute(Constants.REFERENCE_DAY_KEY, cal.getTime());
        }
        else if (form.isDoNextMonth())
        {
            if (log.isDebugEnabled())
                log.debug("go to next month");

            java.util.Calendar cal = GregorianCalendar.getInstance();
            cal.setTime(calendarView.getReferenceDate());
            if (cal.get(java.util.Calendar.MONTH) == java.util.Calendar.DECEMBER)
                cal.roll(java.util.Calendar.YEAR, +1);
            cal.roll(java.util.Calendar.MONTH, +1);
            calendarView.setReferenceDate(cal.getTime());

            request.getSession().setAttribute(Constants.REFERENCE_DAY_KEY, cal.getTime());
        }
        else if (update)
        {
            calendarView.update();
        }

        // Pr黤e welche Kalender von anderen Benutzern gelesen werden k鰊nen
        List<CalendarVO> calendars = new CalendarDAO().getAll();

        Iterator<CalendarVO> it = calendars.iterator();
        while (it.hasNext())
        {
            CalendarVO vo = it.next();
            if (vo.getUser().equals(user))
                it.remove();
            else if (!EntityAccessUtility.isAccessGranted(user, vo, EntityAccess.Access.READ))
                it.remove();
        }
        if (calendars.size() > 0)
            request.getSession().setAttribute("calendarNavigation", calendars);
        else
            request.getSession().removeAttribute("calendarNavigation");

        return mapping.getInputForward();
    }

    private CalendarVO getMainCalendar(UserVO user)
    {
        CalendarVO calendar = new CalendarDAO().getForUser(user);

        if (calendar == null)
        {
            try
            {
                CalendarDAO calendarDAO = new CalendarDAO();

                calendar = calendarDAO.getNewInstance();
                calendar.setUser(user);

                // set entity access
                calendar.setUserAccess(EntityAccess.Access.WRITE);
                calendar.setUserOwner(user.getId());
                calendar.setGroupAccess(EntityAccess.Access.READ);
                calendar.setGroupOwner(user.getMainUsergroup().getId());
                calendar.setGlobalAccess(EntityAccess.Access.READ);

                calendarDAO.insert(calendar, user.getId());

                if (log.isDebugEnabled())
                    log.debug("created calendar " + calendar);
            }
            catch (HibernateException e)
            {
                log.error("could not create main calendar for user: " + user, e);
            }
        }

        return calendar;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?