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

📄 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.company.edit;

import java.io.IOException;
import java.util.ArrayList;
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.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.AddressDAO;
import org.opencustomer.application.db.dao.crm.CompanyDAO;
import org.opencustomer.application.db.vo.crm.AddressVO;
import org.opencustomer.application.db.vo.crm.CompanyVO;
import org.opencustomer.application.db.vo.system.UserVO;
import org.opencustomer.application.web.Globals;
import org.opencustomer.application.web.module.crm.company.Constants;
import org.opencustomer.application.web.struts.Action;
import org.opencustomer.application.web.struts.ActionForm;
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))
            {
                CompanyVO company = (CompanyVO) request.getSession().getAttribute(Constants.COMPANY_COMPANY);
                List<AddressVO> addresses = (List<AddressVO>) request.getSession().getAttribute(Constants.COMPANY_ADDRESSES);

                List<AddressVO> removeAddresses = new ArrayList<AddressVO>();

                // entferne der nicht m
                Iterator<AddressVO> it = addresses.iterator();
                while (it.hasNext())
                {
                    AddressVO vo = it.next();
                    if (isAddressEmpty(vo))
                    {
                        if (log.isDebugEnabled())
                            log.debug("remove empty address: " + vo);

                        removeAddresses.add(vo);
                        it.remove();
                    }
                }

                try
                {
                    // speicher die Firma
                    if (company.getId() == null)
                        new CompanyDAO().insert(company, user.getId());
                    else
                        new CompanyDAO().update(company, user.getId());

                    AddressDAO addressDAO = new AddressDAO();

                    // speicher alle Adressen, wenn sie nicht leer sind
                    for (AddressVO address : addresses)
                    {
                        address.setCompany(company);
                        if (address.getId() == null)
                            addressDAO.insert(address, user.getId());
                        else
                            addressDAO.update(address, user.getId());
                    }

                    for (AddressVO address : removeAddresses)
                    {
                        if (address.getId() != null)
                            addressDAO.delete(address, user.getId());
                    }

                }
                catch (HibernateException e)
                {
                    log.error("problems saving company (id:" + company.getId() + ")", e);
                }
            }
            else
            {
                if (log.isDebugEnabled())
                    log.debug("do not save company: 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:
                Integer page = (Integer) request.getSession().getAttribute(Constants.COMPANY_PAGE);

                if (DefaultPageAction.PAGE_INFO.equals(page))
                    return mapping.findForward("company.edit.page2");
                else if (DefaultPageAction.PAGE_PERSONS.equals(page))
                    return mapping.findForward("company.edit.page3");
                else if (DefaultPageAction.PAGE_CONTACTS.equals(page))
                    return mapping.findForward("company.edit.page4");
                else if (DefaultPageAction.PAGE_ADDRESSES.equals(page))
                    return mapping.findForward("company.edit.pageAddress");
                else
                    return mapping.findForward("company.edit.page1");

            default:
                removeAttributes(request);

                if (request.getAttribute(org.opencustomer.application.web.module.crm.person.Constants.PERSON_EXTERNCALL) != null)
                    return mapping.findForward("jumpPerson");
                else if (request.getAttribute(org.opencustomer.application.web.module.crm.company.Constants.COMPANY_EXTERNCALL) != null)
                    return mapping.findForward("jumpCompany");
                else
                    return mapping.findForward("saved");
        }

    }

    private boolean isAddressEmpty(AddressVO address)
    {
        boolean isEmpty = false;

        if (address.getName1() == null && address.getName2() == null && address.getName3() == null && address.getStreet() == null && address.getPostbox() == null && address.getZip() == null && address.getCity() == null)
            isEmpty = true;

        return isEmpty;
    }

    private boolean validateData(HttpServletRequest request, ActionMessages errors)
    {
        CompanyVO company = (CompanyVO) request.getSession().getAttribute(Constants.COMPANY_COMPANY);

        // Abfangen der m鰃lichen Fehler
        if (company.getCompanyName() == null)
        {
            errors.add("companyName", new ActionMessage("default.error.missingInput", MessageUtil.message(request, "entity.crm.company.companyName")));
        }
        else
        {
            try
            {
                CompanyDAO dao = new CompanyDAO();

                CompanyVO namedCompany = dao.getByName(company.getCompanyName());
                if (namedCompany != null && !namedCompany.getId().equals(company.getId()))
                    errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("domain.crm.company.error.nameExists"));

                dao.getSession().evict(namedCompany);
            }
            catch (HibernateException e)
            {
                log.error("could not read company for name: " + company.getCompanyName());
            }
        }

        if (company.getAddresses() != null)
        {
            Iterator it = company.getAddresses().iterator();
            while (it.hasNext())
            {
                AddressVO address = (AddressVO) it.next();

                if (log.isDebugEnabled())
                    log.debug("validate " + address);

                String prefix = null;
                if (AddressVO.Type.COMPANY.equals(address.getType()))
                    prefix = "companyAddress";
                else if (AddressVO.Type.POSTAL.equals(address.getType()))
                    prefix = "postalAddress";
                else if (AddressVO.Type.BILLING.equals(address.getType()))
                    prefix = "billingAddress";
                else if (AddressVO.Type.DELIVERY.equals(address.getType()))
                    prefix = "deliveryAddress";

                if (address.getZip() != null)
                {
                    if (!address.getZip().matches("\\d{5}"))
                        errors.add(prefix + "Zip", new ActionMessage("default.error.invalidFormat.undefined", MessageUtil.message(request, "entity.crm.address.zip")));
                }

                if (address.getStreet() != null && address.getPostbox() != null)
                    errors.add(prefix, new ActionMessage("domain.crm.company.error.streetAndPostboxSelected", MessageUtil.message(request, "entity.crm.address.type." + prefix)));
            }
        }

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

    public static final void removeAttributes(HttpServletRequest request)
    {
        request.getSession().removeAttribute(Constants.COMPANY_COMPANY);
        request.getSession().removeAttribute(Constants.COMPANY_ADDRESSES);

        request.getSession().removeAttribute(Constants.COMPANY_LEGALFORMGROUP_LIST);
        request.getSession().removeAttribute(Constants.COMPANY_SECTOR_LIST);
        request.getSession().removeAttribute(Constants.COMPANY_COMPANYSTATE_LIST);
        request.getSession().removeAttribute(Constants.COMPANY_COMPANYTYPE_LIST);
    }

}

⌨️ 快捷键说明

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