pagestandardform.java

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

JAVA
302
字号
/*******************************************************************************
 * ***** 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.event.edit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.opencustomer.web.util.FormUtility;
import org.opencustomer.web.util.MessageUtil;

public final class PageStandardForm extends DefaultPageForm
{
    private static final long serialVersionUID = 3258690996551430963L;

    private final static Logger log = Logger.getLogger(PageStandardForm.class);

    private String title;

    private String description;

    private String startDate;

    private String endDate;

    private String startTime;

    private String endTime;

    private boolean allDay;

    private boolean occupied;

    private int eventCategoryId;

    public void validate(ActionMapping mapping, ActionMessages errors, HttpServletRequest request)
    {
        setDoPageStandard(null);

        title = FormUtility.adjustParameter(title, 255);
        description = FormUtility.adjustParameter(description);
        startDate = FormUtility.adjustParameter(startDate);
        startTime = FormUtility.adjustParameter(startTime);
        endDate = FormUtility.adjustParameter(endDate);
        endTime = FormUtility.adjustParameter(endTime);

        if (!isTokenValid(request))
        {
            if (log.isDebugEnabled())
                log.debug("token is not valid");
        }
        else if (getDoSave() != null || this.isAnyPage())
        {
            if (title == null)
                errors.add("title", new ActionMessage("default.error.missingInput", MessageUtil.message(request, "entity.calendar.event.title")));

            if (startDate == null)
            {
                errors.add("startDate", new ActionMessage("default.error.missingInput", MessageUtil.message(request, "entity.calendar.event.startDate.date")));
            }
            else
            {
                String format = MessageUtil.message(request, "format.input.date");
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                try
                {
                    Date date = sdf.parse(startDate);
                    startDate = sdf.format(date);
                }
                catch (ParseException e)
                {
                    errors.add("startDate", new ActionMessage("default.error.invalidFormat", MessageUtil.message(request, "entity.calendar.event.startDate.date"), format));
                }
            }

            if (!allDay)
            {
                if (startTime == null)
                {
                    errors.add("startDate", new ActionMessage("default.error.missingInput", MessageUtil.message(request, "entity.calendar.event.startDate.time")));
                }
                else
                {
                    String format = MessageUtil.message(request, "format.input.time");
                    SimpleDateFormat sdf = new SimpleDateFormat(format);
                    try
                    {
                        Date date = sdf.parse(startTime);
                        startTime = sdf.format(date);
                    }
                    catch (ParseException e)
                    {
                        errors.add("startDate", new ActionMessage("default.error.invalidFormat", MessageUtil.message(request, "entity.calendar.event.startDate.time"), format));
                    }
                }
            }

            if (endDate == null)
            {
                errors.add("endDate", new ActionMessage("default.error.missingInput", MessageUtil.message(request, "entity.calendar.event.endDate.date")));
            }
            else
            {
                String format = MessageUtil.message(request, "format.input.date");
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                try
                {
                    Date date = sdf.parse(endDate);
                    endDate = sdf.format(date);
                }
                catch (ParseException e)
                {
                    errors.add("endDate", new ActionMessage("default.error.invalidFormat", MessageUtil.message(request, "entity.calendar.event.endDate.date"), format));
                }
            }

            if (!allDay)
            {
                if (endTime == null)
                {
                    errors.add("endDate", new ActionMessage("default.error.missingInput", MessageUtil.message(request, "entity.calendar.event.endDate.time")));
                }
                else
                {
                    String format = MessageUtil.message(request, "format.input.time");
                    SimpleDateFormat sdf = new SimpleDateFormat(format);
                    try
                    {
                        Date date = sdf.parse(endTime);
                        endTime = sdf.format(date);
                    }
                    catch (ParseException e)
                    {
                        errors.add("endDate", new ActionMessage("default.error.invalidFormat", MessageUtil.message(request, "entity.calendar.event.endDate.time"), format));
                    }
                }
            }

            if (errors.size("startDate") == 0 && errors.size("endDate") == 0)
            {
                String formatDate = MessageUtil.message(request, "format.input.date");
                String formatTime = MessageUtil.message(request, "format.input.time");

                if (allDay)
                {
                    SimpleDateFormat sdf = new SimpleDateFormat(formatDate);

                    try
                    {
                        Date start = sdf.parse(startDate);
                        Date end = sdf.parse(endDate);

                        if (end.before(start))
                            errors.add("endDate", new ActionMessage("domain.calendar.event.error.endBeforeStart"));

                    }
                    catch (ParseException e)
                    {
                        log.error("problems parsing contact dates", e);
                    }
                }
                else
                {
                    SimpleDateFormat sdf = new SimpleDateFormat(formatDate + " '-' " + formatTime);

                    try
                    {
                        Date start = sdf.parse(startDate + " - " + startTime);
                        Date end = sdf.parse(endDate + " - " + endTime);

                        if (!end.after(start))
                            errors.add("endDate", new ActionMessage("domain.calendar.event.error.endBeforeStart"));

                    }
                    catch (ParseException e)
                    {
                        log.error("problems parsing contact dates", e);
                    }
                }

            }
        }
    }

    public final String getDescription()
    {
        return description;
    }

    public final void setDescription(String description)
    {
        this.description = description;
    }

    public final String getEndDate()
    {
        return endDate;
    }

    public final void setEndDate(String endDate)
    {
        this.endDate = endDate;
    }

    public final String getStartDate()
    {
        return startDate;
    }

    public final void setStartDate(String startDate)
    {
        this.startDate = startDate;
    }

    public final String getTitle()
    {
        return title;
    }

    public final void setTitle(String title)
    {
        this.title = title;
    }

    public final boolean isAllDay()
    {
        return allDay;
    }

    public final void setAllDay(boolean allDay)
    {
        this.allDay = allDay;
    }

    public final int getEventCategoryId()
    {
        return eventCategoryId;
    }

    public final void setEventCategoryId(int eventCategoryId)
    {
        this.eventCategoryId = eventCategoryId;
    }

    public final boolean isOccupied()
    {
        return occupied;
    }

    public final void setOccupied(boolean occupied)
    {
        this.occupied = occupied;
    }

    public final String getEndTime()
    {
        return endTime;
    }

    public final void setEndTime(String endTime)
    {
        this.endTime = endTime;
    }

    public final String getStartTime()
    {
        return startTime;
    }

    public final void setStartTime(String startTime)
    {
        this.startTime = startTime;
    }

}

⌨️ 快捷键说明

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