Java tutorial
package com.jeans.iservlet.service.asset.impl; import java.io.Serializable; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; 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.Asset; import com.jeans.iservlet.model.asset.Hardware; import com.jeans.iservlet.model.asset.Software; import com.jeans.iservlet.model.hr.Company; import com.jeans.iservlet.model.hr.Employee; import com.jeans.iservlet.model.hr.HRUnit; import com.jeans.iservlet.service.asset.AssetService; import com.jeans.iservlet.view.asset.AssetValidateResult; @Service public class AssetServiceImpl implements AssetService { private BaseDao<Asset> asDao; private BaseDao<Hardware> hwDao; private BaseDao<Software> swDao; @Autowired public void setAsDao(BaseDao<Asset> asDao) { this.asDao = asDao; } @Autowired public void setHwDao(BaseDao<Hardware> hwDao) { this.hwDao = hwDao; } @Autowired public void setSwDao(BaseDao<Software> swDao) { this.swDao = swDao; } @Override @Transactional(readOnly = true) public Asset loadAsset(long id, byte type) { Asset asset = null; if (id > 0) { if (type == AssetConstants.HARDWARE_ASSET) { asset = hwDao.getById(Hardware.class, id); } else if (type == AssetConstants.SOFTWARE_ASSET) { asset = swDao.getById(Software.class, id); } else { asset = asDao.getById(Asset.class, id); } } return asset; } @Override @Transactional(readOnly = true) public List<Asset> loadAssets(Set<Long> ids, byte type) { List<Asset> assets = new ArrayList<Asset>(); for (long id : ids) { assets.add(loadAsset(id, type)); } return assets; } @Override @Transactional(readOnly = true) public List<Asset> loadAssets(Company company, byte catalog, int page, int rows, long[] total) { List<Asset> assets = new ArrayList<Asset>(); total[0] = 0; StringBuilder hql = new StringBuilder("from "); Map<String, Object> params = new HashMap<String, Object>(); params.put("p_company", company); boolean isHardware = true; if (catalog == AssetConstants.HARDWARE_ASSET) { hql.append("Hardware where "); } else if (catalog == AssetConstants.SOFTWARE_ASSET) { hql.append("Software where "); isHardware = false; } else if (catalog >= AssetConstants.NETWORK_EQUIPMENT && catalog <= AssetConstants.OTHER_EQUIPMENT) { hql.append("Hardware where catalog = :p_catalog and "); params.put("p_catalog", catalog); } else if (catalog >= AssetConstants.OPERATING_SYSTEM_SOFTWARE && catalog <= AssetConstants.OTHER_SOFTWARE) { hql.append("Software where catalog = :p_catalog and "); params.put("p_catalog", catalog); isHardware = false; } else { return assets; } hql.append("company = :p_company"); if (isHardware) { assets.addAll(hwDao.find(hql.toString(), params, page, rows)); total[0] = hwDao.count(hql.toString(), params); } else { assets.addAll(swDao.find(hql.toString(), params, page, rows)); total[0] = swDao.count(hql.toString(), params); } return assets; } @Override @Transactional(readOnly = true) public List<Asset> loadAssets(Company company, byte catalog) { List<Asset> assets = new ArrayList<Asset>(); StringBuilder hql = new StringBuilder("from "); Map<String, Object> params = new HashMap<String, Object>(); params.put("p_company", company); boolean isHardware = true; if (catalog == AssetConstants.HARDWARE_ASSET) { hql.append("Hardware where "); } else if (catalog == AssetConstants.SOFTWARE_ASSET) { hql.append("Software where "); isHardware = false; } else if (catalog >= AssetConstants.NETWORK_EQUIPMENT && catalog <= AssetConstants.OTHER_EQUIPMENT) { hql.append("Hardware where catalog = :p_catalog and "); params.put("p_catalog", catalog); } else if (catalog >= AssetConstants.OPERATING_SYSTEM_SOFTWARE && catalog <= AssetConstants.OTHER_SOFTWARE) { hql.append("Software where catalog = :p_catalog and "); params.put("p_catalog", catalog); isHardware = false; } hql.append("company = :p_company order by catalog, id"); if (isHardware) { assets.addAll(hwDao.find(hql.toString(), params)); } else { assets.addAll(swDao.find(hql.toString(), params)); } return assets; } @Override @Transactional public Asset newAsset(Map<String, Object> properties) { Asset asset = Asset.createAsset(properties); if (null != asset) { if (asset instanceof Hardware) { hwDao.save((Hardware) asset); } else if (asset instanceof Software) { swDao.save((Software) asset); } } return asset; } @Override @Transactional public int saveProps(Set<Long> ids, byte type, Map<String, Object> props) { if (ids.isEmpty()) { return 0; } if (ids.size() == 1) { Asset asset = loadAsset(ids.iterator().next(), type); if (null == asset) { return 0; } /* * ? ?? null?null -99???? ? */ asset.setName((String) props.get("name")); asset.setVendor((String) props.get("vendor")); asset.setModelOrVersion((String) props.get("modelOrVersion")); asset.setAssetUsage((String) props.get("assetUsage")); asset.setPurchaseTime((Date) props.get("purchaseTime")); int q = (int) props.get("quantity"); asset.setQuantity(q < 0 ? 1 : q); BigDecimal c = (BigDecimal) props.get("cost"); asset.setCost(c.compareTo(BigDecimal.ZERO) == -1 ? BigDecimal.ZERO : c); asset.setComment((String) props.get("comment")); if (asset instanceof Hardware) { ((Hardware) asset).setCode((String) props.get("code")); ((Hardware) asset).setFinancialCode((String) props.get("financialCode")); ((Hardware) asset).setSn((String) props.get("sn")); ((Hardware) asset).setConfiguration((String) props.get("configuration")); byte w = (byte) props.get("warranty"); if (w >= -1 && w <= 1) { ((Hardware) asset).setWarranty(w); } ((Hardware) asset).setLocation((String) props.get("location")); ((Hardware) asset).setIp((String) props.get("ip")); byte i = (byte) props.get("importance"); if (i >= 0 && i <= 2) { ((Hardware) asset).setImportance(i); } hwDao.update((Hardware) asset); return 1; } else if (asset instanceof Software) { byte s = (byte) props.get("softwareType"); if (s >= 0 && s <= 6) { ((Software) asset).setSoftwareType(s); } ((Software) asset).setLicense((String) props.get("license")); ((Software) asset).setExpiredTime((Date) props.get("expiredTime")); swDao.update((Software) asset); return 1; } else { return 0; } } else { List<Asset> assets = loadAssets(ids, type); int count = assets.size(); for (Asset asset : assets) { /* * ????? null????? -99????? ????? */ String v = (String) props.get("name"); if (!StringUtils.isEmpty(v)) { asset.setName(v); } v = (String) props.get("vendor"); if (!StringUtils.isEmpty(v)) { asset.setVendor(v); } v = (String) props.get("modelOrVersion"); if (!StringUtils.isEmpty(v)) { asset.setModelOrVersion(v); } v = (String) props.get("assetUsage"); if (!StringUtils.isEmpty(v)) { asset.setAssetUsage(v); } Date d = (Date) props.get("purchaseTime"); if (null != d) { asset.setPurchaseTime(d); } int q = (int) props.get("quantity"); if (q >= 0) { asset.setQuantity(q); } BigDecimal c = (BigDecimal) props.get("cost"); if (c.compareTo(BigDecimal.ZERO) != -1) { asset.setCost(c); } v = (String) props.get("comment"); if (!StringUtils.isEmpty(v)) { asset.setComment(v); } if (asset instanceof Hardware) { v = (String) props.get("sn"); if (!StringUtils.isEmpty(v)) { ((Hardware) asset).setSn(v); } v = (String) props.get("configuration"); if (!StringUtils.isEmpty(v)) { ((Hardware) asset).setConfiguration(v); } byte w = (byte) props.get("warranty"); if (w >= -1 && w <= 1) { ((Hardware) asset).setWarranty(w); } v = (String) props.get("location"); if (!StringUtils.isEmpty(v)) { ((Hardware) asset).setLocation(v); } v = (String) props.get("ip"); if (!StringUtils.isEmpty(v)) { ((Hardware) asset).setIp(v); } byte i = (byte) props.get("importance"); if (i >= 0 && i <= 2) { ((Hardware) asset).setImportance(i); } hwDao.update((Hardware) asset); } else if (asset instanceof Software) { byte s = (byte) props.get("softwareType"); if (s >= 0 && s <= 6) { ((Software) asset).setSoftwareType(s); } v = (String) props.get("license"); if (!StringUtils.isEmpty(v)) { ((Software) asset).setLicense(v); } d = (Date) props.get("expiredTime"); if (null != d) { ((Software) asset).setExpiredTime(d); } swDao.update((Software) asset); } else { count--; } } return count; } } @Override @Transactional(readOnly = true) public List<AssetValidateResult> validate(Company company) { List<AssetValidateResult> results = new ArrayList<AssetValidateResult>(); String hql = "from Hardware where company = :p_company"; Map<String, Object> params = new HashMap<String, Object>(); params.put("p_company", company); List<Hardware> assets = hwDao.find(hql, params); for (Hardware hardware : assets) { if (null == hardware.getOwner()) { // if (hardware.getState() == AssetConstants.IN_USE) { // results.add(new AssetValidateResult(AssetConstants.IN_USE_ASSET_WITHOUT_OWNER, hardware.getId(), AssetConstants.getAssetCatalogName(hardware.getCatalog()), hardware.getFullName(), null, null)); } } else { // Employee owner = hardware.getOwner(); if (!owner.getDepartment().getCompany().equals(hardware.getCompany())) { // ?? results.add(new AssetValidateResult(AssetConstants.INVALID_OWNER_COMPANY, hardware.getId(), AssetConstants.getAssetCatalogName(hardware.getCatalog()), hardware.getFullName(), owner.getName(), owner.getDepartment().getCompany().getAlias())); } else { if (owner.getStatus() == HRUnit.HISTORY && hardware.getState() == AssetConstants.IN_USE) { // ?? results.add(new AssetValidateResult(AssetConstants.IN_USE_ASSET_OWNED_BY_FORMER_EMPLOYEE, hardware.getId(), AssetConstants.getAssetCatalogName(hardware.getCatalog()), hardware.getFullName(), owner.getName(), owner.getDepartment().getCompany().getAlias())); } } } } return results; } @Override @Transactional(readOnly = true) public Set<Byte> checkNextStates(Set<Long> ids, byte type) { Set<Byte> states = new HashSet<Byte>(); if (ids.size() == 0) { return states; } states.add(AssetConstants.IN_USE); states.add(AssetConstants.IDLE); states.add(AssetConstants.DISUSE); if (type == AssetConstants.HARDWARE_ASSET) { states.add(AssetConstants.FIXING); states.add(AssetConstants.ELIMINATED); } // ? Set<Byte> n_h_iu = new HashSet<Byte>(); Set<Byte> n_h_id = new HashSet<Byte>(); Set<Byte> n_h_fx = new HashSet<Byte>(); Set<Byte> n_h_du = new HashSet<Byte>(); Set<Byte> n_s_iu = new HashSet<Byte>(); Set<Byte> n_s_id = new HashSet<Byte>(); n_h_iu.add(AssetConstants.IN_USE); n_h_iu.add(AssetConstants.IDLE); n_h_iu.add(AssetConstants.FIXING); n_h_id.add(AssetConstants.IN_USE); n_h_id.add(AssetConstants.FIXING); n_h_id.add(AssetConstants.DISUSE); n_h_fx.add(AssetConstants.IN_USE); n_h_fx.add(AssetConstants.IDLE); n_h_fx.add(AssetConstants.DISUSE); n_h_du.add(AssetConstants.ELIMINATED); n_s_iu.add(AssetConstants.IDLE); n_s_iu.add(AssetConstants.DISUSE); n_s_id.add(AssetConstants.IN_USE); n_s_id.add(AssetConstants.DISUSE); List<Asset> assets = loadAssets(ids, type); if (type == AssetConstants.HARDWARE_ASSET) { /* * (???) -> // -> // -> //? -> ? ? -> null */ for (Asset asset : assets) { byte oldState = asset.getState(); if (oldState == AssetConstants.ELIMINATED) { // ???? states.clear(); break; } else { if (states.size() == 0) { // ????? break; } else { switch (oldState) { case AssetConstants.IN_USE: states.retainAll(n_h_iu); break; case AssetConstants.IDLE: states.retainAll(n_h_id); break; case AssetConstants.FIXING: states.retainAll(n_h_fx); break; case AssetConstants.DISUSE: states.retainAll(n_h_du); } } } } } else { /* * -> / -> / -> null */ for (Asset asset : assets) { byte oldState = asset.getState(); if (oldState == AssetConstants.DISUSE) { // ??? states.clear(); break; } else { if (states.size() == 1) { // ????????????? continue; } else { if (oldState == AssetConstants.IN_USE) { states.retainAll(n_s_iu); } else { states.retainAll(n_s_id); } } } } } return states; } /** * ?0(IN_USE)ownerkeepOldOwner == true?owner != null????<br> * ??0,ownernullkeepOldOwner == true????<br> * ???? */ @Override @Transactional public int changeState(Set<Long> ids, byte type, byte newState, Employee owner, boolean keepOldOwner) { int count = 0; if (ids.size() > 0) { if (type == AssetConstants.SOFTWARE_ASSET) { for (long id : ids) { Software sw = swDao.getById(Software.class, id); if (null != sw) { sw.setState(newState); swDao.update(sw); count++; } } } else if (type == AssetConstants.HARDWARE_ASSET) { for (long id : ids) { Hardware hw = hwDao.getById(Hardware.class, id); if (null != hw) { hw.setState(newState); if (keepOldOwner) { if (AssetConstants.IN_USE == newState && null == hw.getOwner()) { hw.setOwner(owner); } } else { if (AssetConstants.IN_USE == newState) { hw.setOwner(owner); } else { hw.setOwner(null); } } hwDao.update(hw); count++; } } } } return count; } @Override @Transactional public int adjust(long id, byte adjustType, Employee owner) { Hardware hw = hwDao.getById(Hardware.class, id); int c = 0; if (null != hw) { switch (adjustType) { case 0: // hw.setOwner(owner); hwDao.update(hw); c = 1; break; case 1: // ?IDLEid0 hw.setOwner(null); hw.setState(AssetConstants.IDLE); hwDao.update(hw); c = 1; break; case 2: // ?? Employee o = hw.getOwner(); if (null != o) { hw.setCompany(o.getDepartment().getCompany()); hwDao.update(hw); c = 1; } } } return c; } @Override @Transactional public int createNewAssets(Map<String, Object> props) { byte type = (byte) props.get("type"); int number = (int) props.get("number"); Asset prototype = Asset.createAsset(props); Set<Serializable> ids = new HashSet<Serializable>(); if (null != prototype) { if (number > 1) { if (prototype instanceof Hardware) { DecimalFormat df = new DecimalFormat("000"); String prototypeCode = ((Hardware) prototype).getCode(); for (int n = 1; n <= number; n++) { Hardware hw = new Hardware(); BeanUtils.copyProperties(prototype, hw); hw.setCode(prototypeCode + "[" + df.format(n) + "]"); ids.add(hwDao.save(hw)); } } else { while (number-- > 0) { Software sw = new Software(); BeanUtils.copyProperties(prototype, sw); ids.add(swDao.save(sw)); } } } else { if (type == AssetConstants.HARDWARE_ASSET) { ids.add(hwDao.save((Hardware) prototype)); } else { ids.add(swDao.save((Software) prototype)); } } ids.remove(null); } return ids.size(); } @Override @Transactional(readOnly = true) public List<Hardware> loadEquipmentsByOwner(Employee owner) { List<Hardware> assets = new ArrayList<Hardware>(); if (null != owner) { String hql = "from Hardware where owner = :p_owner order by catalog, code, id"; Map<String, Object> params = new HashMap<String, Object>(); params.put("p_owner", owner); assets = hwDao.find(hql, params); } return assets; } }