com.ewcms.content.particular.service.ProjectBasicService.java Source code

Java tutorial

Introduction

Here is the source code for com.ewcms.content.particular.service.ProjectBasicService.java

Source

/**
 * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved.
 * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * http://www.ewcms.com
 */
package com.ewcms.content.particular.service;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ewcms.content.particular.BaseException;
import com.ewcms.content.particular.dao.ApprovalRecordDAO;
import com.ewcms.content.particular.dao.IndustryCodeDAO;
import com.ewcms.content.particular.dao.ProjectArticleDAO;
import com.ewcms.content.particular.dao.ProjectBasicDAO;
import com.ewcms.content.particular.dao.ZoningCodeDAO;
import com.ewcms.content.particular.model.ApprovalRecord;
import com.ewcms.content.particular.model.IndustryCode;
import com.ewcms.content.particular.model.ProjectArticle;
import com.ewcms.content.particular.model.ProjectBasic;
import com.ewcms.content.particular.model.ProjectBasic.Nature;
import com.ewcms.content.particular.model.ProjectBasic.Shape;
import com.ewcms.content.particular.model.ZoningCode;
import com.ewcms.content.particular.util.XmlConvert;
import com.ewcms.core.site.SiteFacable;
import com.ewcms.core.site.model.Organ;

@Service
public class ProjectBasicService implements ProjectBasicServiceable {

    @Autowired
    private ProjectArticleDAO projectArticleDAO;
    @Autowired
    private ProjectBasicDAO projectBasicDAO;
    @Autowired
    private ZoningCodeDAO zoningCodeDAO;
    @Autowired
    private ApprovalRecordDAO approvalRecordDAO;
    @Autowired
    private IndustryCodeDAO industryCodeDAO;
    @Autowired
    private SiteFacable siteFac;

    @Override
    public Long addProjectBasic(ProjectBasic projectBasic) throws BaseException {
        String code = generateCode(projectBasic);

        ProjectBasic entity = projectBasicDAO.findProjectBasicByCode(code);
        if (entity != null)
            throw new BaseException("??", "??");

        projectBasic.setCode(code);
        setZoningCode(projectBasic);
        setApprovalRecord(projectBasic);
        setIndustryCode(projectBasic);
        setOrgan(projectBasic);

        projectBasicDAO.persist(projectBasic);
        return projectBasic.getId();
    }

    private String generateCode(ProjectBasic projectBasic) {
        String zoningCode_code = projectBasic.getZoningCode().getCode();
        String organizationCode_code = projectBasic.getOrganizationCode();
        Calendar ca = Calendar.getInstance();
        ca.setTime(projectBasic.getBuildTime());
        String year = String.valueOf(ca.get(Calendar.YEAR));
        String unitId = projectBasic.getUnitId();
        String code = zoningCode_code + organizationCode_code + year + unitId;

        return code;
    }

    @Override
    public Long updProjectBasic(ProjectBasic projectBasic) throws BaseException {
        String code_new = generateCode(projectBasic);
        String code_old = projectBasic.getCode();

        projectBasic.setCode(code_new);
        setZoningCode(projectBasic);
        setApprovalRecord(projectBasic);
        setIndustryCode(projectBasic);
        setOrgan(projectBasic);
        projectBasic.setRelease(false);

        if (code_new.equals(code_old)) {
            projectBasicDAO.merge(projectBasic);
        } else {
            ProjectBasic entity = projectBasicDAO.findProjectBasicByCode(code_new);
            if (entity != null)
                throw new BaseException("??", "??");

            List<ProjectArticle> articles = projectBasicDAO.findProjectArticleByBasicId(code_old);
            if (articles != null && !articles.isEmpty()) {
                for (ProjectArticle article : articles) {
                    article.setProjectBasic(projectBasic);
                    projectArticleDAO.merge(article);
                }
            } else {
                projectBasicDAO.merge(projectBasic);
            }
        }
        return projectBasic.getId();
    }

    private void setZoningCode(ProjectBasic projectBasic) {
        String zoningCode_code = projectBasic.getZoningCode().getCode();
        Assert.notNull(zoningCode_code);
        ZoningCode zoningCode = zoningCodeDAO.findZoningCodeByCode(zoningCode_code);
        Assert.notNull(zoningCode);
        projectBasic.setZoningCode(zoningCode);
    }

    private void setApprovalRecord(ProjectBasic projectBasic) {
        String approvalRecord_code = projectBasic.getApprovalRecord().getCode();
        ApprovalRecord approvalRecord = null;
        if (approvalRecord_code != null && approvalRecord_code.length() > 0) {
            approvalRecord = approvalRecordDAO.findApprovalRecordByCode(approvalRecord_code);
        }
        projectBasic.setApprovalRecord(approvalRecord);
    }

    private void setIndustryCode(ProjectBasic projectBasic) {
        String industryCode_code = projectBasic.getIndustryCode().getCode();
        Assert.notNull(industryCode_code);
        IndustryCode industryCode = industryCodeDAO.findIndustryCodeByCode(industryCode_code);
        Assert.notNull(industryCode);
        projectBasic.setIndustryCode(industryCode);
    }

    private void setOrgan(ProjectBasic projectBasic) {
        Integer organId = projectBasic.getOrgan().getId();
        Organ organ = siteFac.getOrgan(organId);
        projectBasic.setOrgan(organ);
    }

    @Override
    public void delProjectBasic(Long id) {
        projectBasicDAO.removeByPK(id);
    }

    @Override
    public ProjectBasic findProjectBasicById(Long id) {
        return projectBasicDAO.get(id);
    }

    @Override
    public List<ProjectBasic> findProjectBasicByPageAndRows(Integer page, Integer rows, String name) {
        return projectBasicDAO.findProjectBasicByPageAndRows(page, rows, name);
    }

    @Override
    public Long findProjectBasicTotal(String name) {
        return projectBasicDAO.findProjectBasicTotal(name);
    }

    @Override
    public void addProjectBasicByImportXml(File file, Integer channelId, String fileType) {
        List<Map<String, Object>> list = XmlConvert.importXML(file, fileType);

        if (list.isEmpty())
            return;

        for (Map<String, Object> map : list) {
            ProjectBasic projectBasic = new ProjectBasic();

            String zoningCode_code = (String) map.get("?");
            ZoningCode zoningCode = zoningCodeDAO.findZoningCodeByCode(zoningCode_code);
            if (zoningCode == null)
                continue;
            projectBasic.setZoningCode(zoningCode);

            String organizationCode = (String) map.get("?");
            projectBasic.setOrganizationCode(organizationCode);

            String buildTime = (String) map.get("");
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                projectBasic.setBuildTime(dateFormat.parse(buildTime));
            } catch (ParseException e) {
                continue;
            }

            String unitId = (String) map.get("???");
            projectBasic.setUnitId(unitId);

            String generateCode = generateCode(projectBasic);
            projectBasic.setCode(generateCode);
            ProjectBasic projectBasic_db = projectBasicDAO.findProjectBasicByCode(generateCode);
            if (projectBasic_db != null) {
                projectBasic.setId(projectBasic_db.getId());
                projectBasic.setOrgan(projectBasic_db.getOrgan());
            }

            String bildNature = (String) map.get("");
            for (Nature nature : Nature.values()) {
                if (nature.getDescription().trim().equals(bildNature.trim())) {
                    projectBasic.setBildNature(nature);
                    break;
                }
            }

            String industryCode_code = (String) map.get("?");
            IndustryCode industryCode = industryCodeDAO.findIndustryCodeByCode(industryCode_code);
            if (industryCode == null)
                continue;
            projectBasic.setIndustryCode(industryCode);

            String approvalRecord_code = (String) map.get("?");
            ApprovalRecord approvalRecord = approvalRecordDAO.findApprovalRecordByCode(approvalRecord_code);
            projectBasic.setApprovalRecord(approvalRecord);

            projectBasic.setChannelId(channelId);

            String name = (String) map.get("??");
            projectBasic.setName(name);

            String investmentScale = (String) map.get("");
            projectBasic.setInvestmentScale(investmentScale);

            String overview = (String) map.get("");
            projectBasic.setOverview(overview);

            String buildUnit = (String) map.get("??");
            projectBasic.setBuildUnit(buildUnit);

            String unitPhone = (String) map.get("????");
            projectBasic.setUnitPhone(unitPhone);

            String unitAddress = (String) map.get("???");
            projectBasic.setUnitAddress(unitAddress);

            String category = (String) map.get("");
            projectBasic.setCategory(category);

            String address = (String) map.get("?");
            projectBasic.setAddress(address);

            String contact = (String) map.get("?");
            projectBasic.setContact(contact);

            String phone = (String) map.get("??");
            projectBasic.setPhone(phone);

            String email = (String) map.get("??");
            projectBasic.setEmail(email);

            String shape = (String) map.get("?");
            if (shape != null) {
                for (Shape value : Shape.values()) {
                    if (value.getDescription().trim().equals(shape.trim())) {
                        projectBasic.setShape(value);
                        break;
                    }
                }
            }

            String documentId = (String) map.get("?");
            projectBasic.setDocumentId(documentId);

            String participation = (String) map.get("???");
            projectBasic.setParticipation(participation);

            if (projectBasic.getId() == null) {
                projectBasicDAO.persist(projectBasic);
            } else {
                projectBasicDAO.merge(projectBasic);
            }
        }
    }

    public Document exportXml(List<Long> projectBasicIds) {
        Document document = DocumentHelper.createDocument();

        Element root = document.addElement("MetaDatas");
        Element metaViewData = root.addElement("MetaViewData");

        if (projectBasicIds.isEmpty())
            return document;

        for (Long projectBasicId : projectBasicIds) {
            ProjectBasic projectBasic = projectBasicDAO.get(projectBasicId);

            Element projects = metaViewData.addElement("PROPERTIES");

            Element code = projects.addElement("?");
            code.addText(projectBasic.getCode() == null ? "" : projectBasic.getCode());

            Element name = projects.addElement("??");
            name.addText(projectBasic.getName() == null ? "" : projectBasic.getName());

            Element buildTime = projects.addElement("");
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (projectBasic.getBuildTime() != null) {
                buildTime.addText(dateFormat.format(projectBasic.getBuildTime()));
            } else {
                buildTime.addText("");
            }

            Element investmentScale = projects.addElement("");
            investmentScale
                    .addText(projectBasic.getInvestmentScale() == null ? "" : projectBasic.getInvestmentScale());

            Element overview = projects.addElement("");
            overview.addText(projectBasic.getOverview() == null ? "" : projectBasic.getOverview());

            Element buildUnit = projects.addElement("??");
            buildUnit.addText(projectBasic.getBuildUnit() == null ? "" : projectBasic.getBuildUnit());

            Element unitPhone = projects.addElement("????");
            unitPhone.addText(projectBasic.getUnitPhone() == null ? "" : projectBasic.getUnitPhone());

            Element unitAddress = projects.addElement("???");
            unitAddress.addText(projectBasic.getUnitAddress() == null ? "" : projectBasic.getUnitAddress());

            Element zoningCode_code = projects.addElement("?");
            if (projectBasic.getZoningCode() != null)
                zoningCode_code.addText(projectBasic.getZoningCode().getCode() == null ? ""
                        : projectBasic.getZoningCode().getCode());
            else
                zoningCode_code.addText("");

            Element organizationCode = projects.addElement("?");
            organizationCode
                    .addText(projectBasic.getOrganizationCode() == null ? "" : projectBasic.getOrganizationCode());

            Element industryCode_code = projects.addElement("?");
            if (projectBasic.getIndustryCode() != null)
                industryCode_code.addText(projectBasic.getIndustryCode().getCode() == null ? ""
                        : projectBasic.getIndustryCode().getCode());
            else
                industryCode_code.addText("");

            Element category = projects.addElement("");
            category.addText(projectBasic.getCategory() == null ? "" : projectBasic.getCategory());

            Element unitId = projects.addElement("???");
            unitId.addText(projectBasic.getUnitId() == null ? "" : projectBasic.getUnitId());

            Element approvalRecord_code = projects.addElement("?");
            if (projectBasic.getApprovalRecord() != null)
                approvalRecord_code.addText(projectBasic.getApprovalRecord().getCode() == null ? ""
                        : projectBasic.getApprovalRecord().getCode());
            else
                approvalRecord_code.addText("");

            Element address = projects.addElement("?");
            address.addText(projectBasic.getAddress() == null ? "" : projectBasic.getAddress());

            Element bildNature = projects.addElement("");
            if (projectBasic.getBildNature() != null)
                bildNature.addText(projectBasic.getBildNature().getDescription() == null ? ""
                        : projectBasic.getBildNature().getDescription());
            else
                bildNature.addText("");

            Element contact = projects.addElement("?");
            contact.addText(projectBasic.getContact() == null ? "" : projectBasic.getContact());

            Element phone = projects.addElement("??");
            phone.addText(projectBasic.getPhone() == null ? "" : projectBasic.getPhone());

            Element email = projects.addElement("??");
            email.addText(projectBasic.getEmail() == null ? "" : projectBasic.getEmail());
        }
        return document;
    }

    @Override
    public void pubProjectBasic(List<Long> projectBasicIds) {
        if (projectBasicIds.isEmpty())
            return;
        for (Long projectBasicId : projectBasicIds) {
            ProjectBasic projectBasic = projectBasicDAO.get(projectBasicId);
            if (projectBasic.getRelease() || projectBasic.getOrgan() == null)
                continue;
            projectBasic.setRelease(true);
            projectBasicDAO.merge(projectBasic);
        }
    }

    @Override
    public void unPubProjectBasic(List<Long> projectBasicIds) {
        if (projectBasicIds.isEmpty())
            return;
        for (Long projectBasicId : projectBasicIds) {
            ProjectBasic projectBasic = projectBasicDAO.get(projectBasicId);
            if (!projectBasic.getRelease())
                continue;
            projectBasic.setRelease(false);
            projectBasicDAO.merge(projectBasic);
        }
    }
}