⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saveaction.java

📁 开源项目CRM之OpenCustomer
💻 JAVA
字号:
/*******************************************************************************
 * ***** 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.crm.contact.edit;

import java.io.IOException;
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.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.hibernate.HibernateException;
import org.opencustomer.application.auth.AuthenticatorUtility;
import org.opencustomer.application.auth.Right;
import org.opencustomer.application.db.dao.crm.ContactDAO;
import org.opencustomer.application.db.dao.crm.PersonContactDAO;
import org.opencustomer.application.db.vo.crm.ContactVO;
import org.opencustomer.application.db.vo.crm.PersonContactVO;
import org.opencustomer.application.db.vo.system.UserVO;
import org.opencustomer.application.web.Globals;
import org.opencustomer.application.web.module.crm.contact.Constants;
import org.opencustomer.application.web.struts.Action;
import org.opencustomer.application.web.struts.ActionForm;
import org.opencustomer.db.Database;
import org.opencustomer.web.util.MessageUtil;

public class SaveAction extends Action
{
    private static Logger log = Logger.getLogger(SaveAction.class);

    private static final int STATUS_SAVED = 0;

    private static final int STATUS_ERROR = 1;

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        ActionMessages errors = new ActionMessages();

        int status = STATUS_SAVED;
        UserVO user = (UserVO) request.getSession().getAttribute(Globals.USER_KEY);

        if (validateData(request, errors))
        {
            if (AuthenticatorUtility.getInstance().authenticate(request, Right.CRM_COMPANIES_WRITE))
            {
                try
                {
                    ContactVO contact = (ContactVO) request.getSession().getAttribute(Constants.CONTACT_CONTACT);
                    List<PersonContactVO> personContacts = (List<PersonContactVO>) request.getSession().getAttribute(Constants.CONTACT_PERSONS);

                    ContactDAO contactDAO = new ContactDAO();
                    if (contact.getId() == null)
                    {
                        if (log.isDebugEnabled())
                            log.debug("create contact " + contact);
                        contactDAO.insert(contact, user.getId());
                    }
                    else
                    {
                        if (log.isDebugEnabled())
                            log.debug("save contact " + contact);
                        contactDAO.update(contact, user.getId());
                    }

                    // entferne nicht mehr ben鰐igte entit鋞en
                    PersonContactDAO personContactDAO = new PersonContactDAO();
                    for (PersonContactVO personContact : contact.getPersonContacts())
                    {
                        if (!personContacts.contains(personContact))
                            personContactDAO.delete(personContact);
                    }

                    // entferne contact, da die personContacts extern
                    // hinzugef黦t werden
                    // das Objekt im Session-Cache ist so n鋗lich veraltet
                    Database.getSession().evict(contact);

                    for (PersonContactVO personContact : personContacts)
                    {
                        if (personContact.getId() == null)
                            personContactDAO.insertOrUpdate(personContact);
                        else
                            personContactDAO.update(personContact);

                    }

                }
                catch (HibernateException e)
                {
                    log.error("could not save contact", e);
                }
            }
            else
            {
                if (log.isDebugEnabled())
                    log.debug("do not save contact: due to invalid rights");
            }
        }
        else
            status = STATUS_ERROR;

        if (!errors.isEmpty())
            saveErrors(request, errors);

        if (log.isDebugEnabled())
            log.debug("status = " + status);

        switch (status)
        {
            case STATUS_ERROR:
                int page = 0;
                if (request.getAttribute("page") != null)
                    page = ((Integer) request.getAttribute("page")).intValue();

                switch (page)
                {
                    default:
                        return mapping.findForward("contact.edit.page1");
                }

            default:
                removeAttributes(request);

                if (request.getSession().getAttribute(org.opencustomer.application.web.module.crm.company.Constants.COMPANY_COMPANY) != null)
                    if (request.getAttribute(org.opencustomer.application.web.module.crm.company.Constants.COMPANY_EXTERNCALL) != null || request.getAttribute(org.opencustomer.application.web.module.crm.person.Constants.PERSON_EXTERNCALL) != null)
                        return mapping.findForward("company.edit.save");
                    else
                        return mapping.findForward("company.edit.page4");
                else if (request.getSession().getAttribute(org.opencustomer.application.web.module.crm.person.Constants.PERSON_PERSON) != null)
                    if (request.getAttribute(org.opencustomer.application.web.module.crm.company.Constants.COMPANY_EXTERNCALL) != null || request.getAttribute(org.opencustomer.application.web.module.crm.person.Constants.PERSON_EXTERNCALL) != null)
                        return mapping.findForward("person.edit.save");
                    else
                        return mapping.findForward("person.edit.pageContact");
                else
                    return mapping.findForward("saved");
        }

    }

    private boolean validateData(HttpServletRequest request, ActionMessages errors)
    {
        ContactVO contact = (ContactVO) request.getSession().getAttribute(Constants.CONTACT_CONTACT);

        if (contact.getSubject() == null)
            errors.add("subject", new ActionMessage("default.error.missingInput", MessageUtil.message(request, "entity.crm.contact.subject")));

        if (errors.isEmpty())
            return true;
        else
            return false;
    }

    public static void removeAttributes(HttpServletRequest request)
    {
        request.getSession().removeAttribute(Constants.CONTACT_CONTACT);
        request.getSession().removeAttribute(Constants.CONTACT_PERSONS);
    }
}

⌨️ 快捷键说明

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