com.jeans.iservlet.service.rest.impl.SoftwareRestServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.jeans.iservlet.service.rest.impl.SoftwareRestServiceImpl.java

Source

package com.jeans.iservlet.service.rest.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeans.iservlet.dao.BaseDao;
import com.jeans.iservlet.model.AssetConstants;
import com.jeans.iservlet.model.asset.AssetChangeLog;
import com.jeans.iservlet.model.asset.Software;
import com.jeans.iservlet.model.hr.Company;
import com.jeans.iservlet.resource.ResourceFactory;
import com.jeans.iservlet.resource.SoftwareResource;
import com.jeans.iservlet.service.rest.SoftwareRestService;

@Service
public class SoftwareRestServiceImpl implements SoftwareRestService {
    @Autowired
    private BaseDao<Software> swDao;

    @Autowired
    private BaseDao<Company> compDao;

    @Autowired
    private BaseDao<AssetChangeLog> aclDao;

    private HttpServletRequest request = null;

    @Override
    @Transactional(readOnly = true)
    public List<SoftwareResource> search(String keyword) {
        List<SoftwareResource> res = new ArrayList<SoftwareResource>();
        if (!StringUtils.isBlank(keyword)) {
            res = ResourceFactory.createResourcesList(Software.class, SoftwareResource.class,
                    search(keyword, null, AssetConstants.SOFTWARE_ASSET));
            try {
                long id = Long.parseLong(keyword);
                Software sw = swDao.getById(Software.class, id);
                if (null != sw) {
                    res.add(ResourceFactory.createResource(Software.class, SoftwareResource.class, sw));
                }
            } catch (NumberFormatException e) {
            }
        }
        return res;
    }

    @Override
    @Transactional(readOnly = true)
    public List<SoftwareResource> search(String keyword, long companyId, byte catalog) {
        List<SoftwareResource> res = new ArrayList<SoftwareResource>();
        Company comp = compDao.getById(Company.class, companyId);
        if (null != comp && !StringUtils.isBlank(keyword)) {
            res = ResourceFactory.createResourcesList(Software.class, SoftwareResource.class,
                    search(keyword, comp, catalog));
            try {
                long id = Long.parseLong(keyword);
                Software sw = swDao.getById(Software.class, id);
                if (null != sw && comp.equals(sw.getCompany())) {
                    res.add(ResourceFactory.createResource(Software.class, SoftwareResource.class, sw));
                }
            } catch (NumberFormatException e) {
            }
        }
        return res;
    }

    /**
     * ??companynull??
     * 
     * @param keyword
     *            
     * @param company
     *            ?null??
     * @param catalog
     *            /
     * @return
     */
    public List<Software> search(String keyword, Company company, byte catalog) {
        StringBuilder hql = new StringBuilder();
        Map<String, Object> params = new HashMap<String, Object>();
        hql.append("from Software where ");
        if (null != company) {
            hql.append("company = :p_comp and ");
            params.put("p_comp", company);
        }
        hql.append("(name like :p_kwx or vendor like :p_kwx or modelOrVersion like :p_kwx)");
        params.put("p_kwx", "%" + keyword + "%");
        if (catalog > 10) {
            hql.append(" and catalog = :p_cata");
            params.put("p_cata", catalog);
        }
        return swDao.find(hql.toString(), params);
    }

    @Override
    @Transactional
    public SoftwareResource create(SoftwareResource resource) {
        if (resource.getId() < 0) {
            resource.setId(0);
            return resource;
        } else {
            Software sw = new Software();
            // 1. company?
            Company company = compDao.getById(Company.class, resource.getCompanyId());
            if (null == company) {
                return null;
            }
            sw.setCompany(company);
            sw.setType(AssetConstants.SOFTWARE_ASSET);
            // 2. catalog?
            byte catalog = resource.getCatalog();
            if ((catalog < AssetConstants.OPERATING_SYSTEM_SOFTWARE
                    || catalog > AssetConstants.APPLICATION_SOFTWARE) && catalog != AssetConstants.OTHER_SOFTWARE) {
                return null;
            }
            sw.setCatalog(catalog);
            // 3. name?
            if (StringUtils.isBlank(resource.getName())) {
                return null;
            }
            sw.setName(StringUtils.left(StringUtils.trimToEmpty(resource.getName()), 32));
            sw.setVendor(StringUtils.left(StringUtils.trimToEmpty(resource.getVendor()), 32));
            sw.setModelOrVersion(StringUtils.left(StringUtils.trimToEmpty(resource.getModelOrVersion()), 64));
            sw.setAssetUsage(StringUtils.left(StringUtils.trimToEmpty(resource.getAssetUsage()), 255));
            sw.setPurchaseTime(resource.getPurchaseTime());
            // 4. quantity?0
            if (resource.getQuantity() <= 0) {
                return null;
            }
            sw.setQuantity(resource.getQuantity());
            // 5. cost?
            if (resource.getCost().doubleValue() < 0) {
                return null;
            }
            sw.setCost(resource.getCost());
            // 6. state?IN_USEIDLE
            byte state = resource.getState();
            if (state != AssetConstants.IN_USE && state != AssetConstants.IDLE) {
                return null;
            }
            sw.setState(state);
            sw.setComment(StringUtils.left(StringUtils.trimToEmpty(resource.getComment()), 255));
            // 7. softwareType?
            byte softwareType = resource.getSoftwareType();
            if (softwareType < AssetConstants.COMMERCIAL_SOFTWARE
                    || softwareType > AssetConstants.OTHER_TYPE_SOFTWARE) {
                return null;
            }
            sw.setSoftwareType(softwareType);
            sw.setLicense(StringUtils.left(StringUtils.trimToEmpty(resource.getLicense()), 64));
            sw.setExpiredTime(resource.getExpiredTime());
            swDao.save(sw);

            return ResourceFactory.createResource(Software.class, SoftwareResource.class, sw);
        }
    }

    @Override
    @Transactional
    public List<SoftwareResource> create(List<SoftwareResource> resources) {
        List<SoftwareResource> created = new ArrayList<SoftwareResource>();
        for (SoftwareResource resource : resources) {
            SoftwareResource sr = create(resource);
            if (null != sr) {
                created.add(sr);
            }
        }
        return created;
    }

    @Override
    @Transactional
    public SoftwareResource delete(SoftwareResource resource) {
        if (resource.getId() < 0) {
            resource.setId(0);
            return resource;
        } else {
            Software sw = swDao.getById(Software.class, resource.getId());
            if (null != sw && sw.getState() != AssetConstants.DISUSE) {
                AssetChangeLog acl = new AssetChangeLog(sw, sw.getState(), AssetConstants.DISUSE, getRequest());
                SoftwareResource deleted = ResourceFactory.createResource(Software.class, SoftwareResource.class,
                        sw);
                sw.setState(AssetConstants.DISUSE);
                swDao.update(sw);
                aclDao.save(acl);
                return deleted;
            } else {
                return null;
            }
        }
    }

    @Override
    @Transactional
    public List<SoftwareResource> delete(List<SoftwareResource> resources) {
        List<SoftwareResource> deleted = new ArrayList<SoftwareResource>();
        for (SoftwareResource resource : resources) {
            SoftwareResource sr = delete(resource);
            if (null != sr) {
                deleted.add(sr);
            }
        }
        return deleted;
    }

    @Override
    @Transactional
    public SoftwareResource[] update(SoftwareResource resource) {
        SoftwareResource[] updates = new SoftwareResource[2];
        if (resource.getId() < 0) {
            updates[UPDATED] = ResourceFactory.createEmptyResource(SoftwareResource.class);
            updates[ORIGIN] = ResourceFactory.createEmptyResource(SoftwareResource.class);
            BeanUtils.copyProperties(resource, updates[UPDATED]);
            BeanUtils.copyProperties(resource, updates[ORIGIN]);
            updates[UPDATED].setId(0);
        } else {
            Software sw = swDao.getById(Software.class, resource.getId());
            if (null != sw && sw.getState() != AssetConstants.DISUSE) {
                AssetChangeLog acl = new AssetChangeLog(sw, sw.getState(), sw.getState(), getRequest());
                updates[ORIGIN] = ResourceFactory.createResource(Software.class, SoftwareResource.class, sw);
                // 1. name?
                if (!StringUtils.isBlank(resource.getName())) {
                    sw.setName(StringUtils.left(StringUtils.trimToEmpty(resource.getName()), 32));
                }
                // 2. catalog?
                byte catalog = resource.getCatalog();
                if ((catalog >= AssetConstants.OPERATING_SYSTEM_SOFTWARE
                        && catalog <= AssetConstants.APPLICATION_SOFTWARE)
                        || catalog == AssetConstants.OTHER_SOFTWARE) {
                    sw.setCatalog(catalog);
                }
                // 3. vender[32], modelOrVersion[64], assetUsage[255], comment[255], license[64], null?
                if (null != resource.getVendor()) {
                    sw.setVendor(StringUtils.left(StringUtils.trimToEmpty(resource.getVendor()), 32));
                }
                if (null != resource.getModelOrVersion()) {
                    sw.setModelOrVersion(
                            StringUtils.left(StringUtils.trimToEmpty(resource.getModelOrVersion()), 64));
                }
                if (null != resource.getAssetUsage()) {
                    sw.setAssetUsage(StringUtils.left(StringUtils.trimToEmpty(resource.getAssetUsage()), 255));
                }
                if (null != resource.getComment()) {
                    sw.setComment(StringUtils.left(StringUtils.trimToEmpty(resource.getComment()), 255));
                }
                if (null != resource.getLicense()) {
                    sw.setLicense(StringUtils.left(StringUtils.trimToEmpty(resource.getLicense()), 64));
                }
                // 4. purchaseTimenull?
                if (null != resource.getPurchaseTime()) {
                    sw.setPurchaseTime(resource.getPurchaseTime());
                }
                // 5. quantity?0?
                if (resource.getQuantity() > 0) {
                    sw.setQuantity(resource.getQuantity());
                }
                // 6. costnull?0?
                if (null != resource.getCost() && resource.getCost().doubleValue() >= 0) {
                    sw.setCost(resource.getCost());
                }
                // 7. state0=, 1=, 3=
                byte state = resource.getState();
                if (state == AssetConstants.IN_USE || state == AssetConstants.IDLE
                        || state == AssetConstants.DISUSE) {
                    acl.setNewState(state);
                    sw.setState(state);
                }
                // 8. softwareType?
                byte softwareType = resource.getSoftwareType();
                if (softwareType >= AssetConstants.COMMERCIAL_SOFTWARE
                        && softwareType <= AssetConstants.OTHER_TYPE_SOFTWARE) {
                    sw.setSoftwareType(softwareType);
                }
                // 9. expiredTimenull?
                if (null != resource.getExpiredTime()) {
                    sw.setExpiredTime(resource.getExpiredTime());
                }
                swDao.update(sw);
                if (acl.isValid()) {
                    aclDao.save(acl);
                }
                updates[UPDATED] = ResourceFactory.createResource(Software.class, SoftwareResource.class, sw);
            }
        }
        return updates;
    }

    @SuppressWarnings("unchecked")
    @Override
    @Transactional
    public List<SoftwareResource>[] update(List<SoftwareResource> resources) {
        List<SoftwareResource>[] updates = new List[] { new ArrayList<SoftwareResource>(),
                new ArrayList<SoftwareResource>() };
        for (SoftwareResource resource : resources) {
            SoftwareResource[] updated = update(resource);
            if (null != updated[UPDATED]) {
                updates[UPDATED].add(updated[UPDATED]);
                updates[ORIGIN].add(updated[ORIGIN]);
            }
        }
        return updates;
    }

    @Override
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public HttpServletRequest getRequest() {
        return request;
    }
}