Java tutorial
package com.jeans.iservlet.action.asset; import java.math.BigDecimal; import java.text.Collator; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.springframework.beans.factory.annotation.Autowired; import com.jeans.iservlet.action.BaseAction; 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.Employee; import com.jeans.iservlet.model.hr.HRUnit; import com.jeans.iservlet.service.asset.AssetService; import com.jeans.iservlet.service.hr.HRService; import com.jeans.iservlet.view.asset.AssetDataListItem; import com.jeans.iservlet.view.asset.AssetItem; import com.jeans.iservlet.view.asset.AssetValidateResult; import com.jeans.iservlet.view.asset.HardwareItem; import com.jeans.iservlet.view.asset.SoftwareItem; import com.jeans.iservlet.view.easyui.DataGrid; import com.jeans.iservlet.view.easyui.DataList; /** * ?Action, : /asset/ * <ul> * <li>load-asset: ?? * <li>load-assets: ??(DataGrid)?? * <li>load-catalog-assets: ??(DataList)??? * <li>load-props: ????? * <li>save-props: ????? * <li>asset-vali: ?? * <li>adjust: ? * <li>check-state: ???(?) * <li>change-state: ??(?) * <li>new-assets: ??? * <li>owned-equipments: ?? * </ul> * * @author majorli * */ public class AssetAction extends BaseAction<DataGrid<AssetItem>> { private AssetService assetService; private HRService hrService; @Autowired public void setAssetService(AssetService assetService) { this.assetService = assetService; } @Autowired public void setHrService(HRService hrService) { this.hrService = hrService; } private byte type; public byte getType() { return type; } public void setType(byte type) { this.type = type; } private AssetItem asset; public AssetItem getAsset() { return asset; } public void setAsset(AssetItem asset) { this.asset = asset; } /** * ?idtype? * * @return * @throws Exception */ @Action(value = "load-asset", results = { @Result(type = "json", params = { "ignoreHierarchy", "false", "root", "asset" }) }) public String loadAsset() throws Exception { Asset a = assetService.loadAsset(id, type); if (null != a) { if (type == AssetConstants.HARDWARE_ASSET) { asset = HardwareItem.createInstance((Hardware) a); } else if (type == AssetConstants.SOFTWARE_ASSET) { asset = SoftwareItem.createInstance((Software) a); } } return SUCCESS; } /** * ? * * @return * @throws Exception */ @Action(value = "load-assets", results = { @Result(type = "json", params = { "root", "data" }) }) public String loadAssets() throws Exception { long[] total = new long[1]; List<Asset> assets = assetService.loadAssets(getCurrentCompany(), type, page, rows, total); List<AssetItem> assetItemsList = new ArrayList<AssetItem>(); for (Asset asset : assets) { if (asset instanceof Hardware) { assetItemsList.add(HardwareItem.createInstance((Hardware) asset)); } else if (asset instanceof Software) { assetItemsList.add(SoftwareItem.createInstance((Software) asset)); } } data = new DataGrid<AssetItem>(total[0], assetItemsList); if (null != sort && data.getRows().size() > 0) { Collections.sort(data.getRows(), new Comparator<AssetItem>() { @Override public int compare(AssetItem o1, AssetItem o2) { int ret = 0; boolean asc = "asc".equals(order); boolean isHardware = o1 instanceof HardwareItem; switch (sort) { case "code": ret = compString(((HardwareItem) o1).getCode(), ((HardwareItem) o2).getCode(), asc); break; case "financialCode": ret = compString(((HardwareItem) o1).getFinancialCode(), ((HardwareItem) o2).getFinancialCode(), asc); break; case "name": if (isHardware) { ret = compString(((HardwareItem) o1).getName(), ((HardwareItem) o2).getName(), asc); } else { ret = compString(((SoftwareItem) o1).getName(), ((SoftwareItem) o2).getName(), asc); } break; case "vendor": if (isHardware) { ret = compString(((HardwareItem) o1).getVendor(), ((HardwareItem) o2).getVendor(), asc); } else { ret = compString(((SoftwareItem) o1).getVendor(), ((SoftwareItem) o2).getVendor(), asc); } break; case "modelOrVersion": if (isHardware) { ret = compString(((HardwareItem) o1).getModelOrVersion(), ((HardwareItem) o2).getName(), asc); } else { ret = compString(((SoftwareItem) o1).getModelOrVersion(), ((SoftwareItem) o2).getModelOrVersion(), asc); } break; case "sn": ret = compString(((HardwareItem) o1).getSn(), ((HardwareItem) o2).getSn(), asc); break; case "purchaseTime": Date t1 = isHardware ? ((HardwareItem) o1).getPurchaseTime() : ((SoftwareItem) o1).getPurchaseTime(); Date t2 = isHardware ? ((HardwareItem) o2).getPurchaseTime() : ((SoftwareItem) o2).getPurchaseTime(); if (null == t1) { if (null == t2) { ret = 0; } else { ret = asc ? -1 : 1; } } else { if (null == t2) { ret = asc ? 1 : -1; } else { ret = asc ? t1.compareTo(t2) : t2.compareTo(t1); } } break; case "quantity": if (isHardware) { ret = asc ? Integer.compare(((HardwareItem) o1).getQuantity(), ((HardwareItem) o2).getQuantity()) : Integer.compare(((HardwareItem) o2).getQuantity(), ((HardwareItem) o1).getQuantity()); } else { ret = asc ? Integer.compare(((SoftwareItem) o1).getQuantity(), ((SoftwareItem) o2).getQuantity()) : Integer.compare(((SoftwareItem) o2).getQuantity(), ((SoftwareItem) o1).getQuantity()); } break; case "cost": BigDecimal d1 = isHardware ? ((HardwareItem) o1).getCost() : ((SoftwareItem) o1).getCost(); BigDecimal d2 = isHardware ? ((HardwareItem) o2).getCost() : ((SoftwareItem) o2).getCost(); if (null == d1) { d1 = new BigDecimal(0.0); } if (null == d2) { d2 = new BigDecimal(0.0); } ret = asc ? d1.compareTo(d2) : d2.compareTo(d1); break; case "state": if (isHardware) { ret = compString(((HardwareItem) o1).getState(), ((HardwareItem) o2).getState(), asc); } else { ret = compString(((SoftwareItem) o1).getState(), ((SoftwareItem) o2).getState(), asc); } break; case "warranty": ret = compString(((HardwareItem) o1).getWarranty(), ((HardwareItem) o2).getWarranty(), asc); break; case "location": ret = compString(((HardwareItem) o1).getLocation(), ((HardwareItem) o2).getLocation(), asc); break; case "ip": ret = compString(((HardwareItem) o1).getIp(), ((HardwareItem) o2).getIp(), asc); break; case "importance": ret = compString(((HardwareItem) o1).getImportance(), ((HardwareItem) o2).getImportance(), asc); break; case "owner": ret = compString(((HardwareItem) o1).getOwner(), ((HardwareItem) o2).getOwner(), asc); break; case "softwareType": ret = compString(((SoftwareItem) o1).getSoftwareType(), ((SoftwareItem) o2).getSoftwareType(), asc); break; case "license": ret = compString(((SoftwareItem) o1).getLicense(), ((SoftwareItem) o2).getLicense(), asc); break; case "expiredTime": Date e1 = ((SoftwareItem) o1).getPurchaseTime(); Date e2 = ((SoftwareItem) o2).getPurchaseTime(); if (null == e1) { if (null == e2) { ret = 0; } else { ret = asc ? -1 : 1; } } else { if (null == e2) { ret = asc ? 1 : -1; } else { ret = asc ? e1.compareTo(e2) : e2.compareTo(e1); } } } return ret; } private int compString(String s1, String s2, boolean asc) { if (null == s1) { if (null == s2) { return 0; } else { return asc ? -1 : 1; } } else { if (null == s2) { return asc ? 1 : -1; } else { return asc ? Collator.getInstance(java.util.Locale.CHINA).compare(s1, s2) : Collator.getInstance(java.util.Locale.CHINA).compare(s2, s1); } } } }); } return SUCCESS; } private DataList<AssetDataListItem> components; public DataList<AssetDataListItem> getComponents() { return components; } public void setComponents(DataList<AssetDataListItem> components) { this.components = components; } /** * ??DataList??? * * @return * @throws Exception */ @Action(value = "load-catalog-assets", results = { @Result(type = "json", params = { "root", "components" }) }) public String listAssetsByCatalog() throws Exception { List<Asset> assets = assetService.loadAssets(getCurrentCompany(), type); components = new DataList<AssetDataListItem>(); for (Asset asset : assets) { components.add(new AssetDataListItem(asset)); } return SUCCESS; } private Map<String, Object> props; public Map<String, Object> getProps() { return props; } public void setProps(Map<String, Object> props) { this.props = props; } /** * ??? * * @return * @throws Exception */ @Action(value = "load-props", results = { @Result(type = "json", params = { "root", "props" }) }) public String loadProps() throws Exception { props = new HashMap<String, Object>(); List<Asset> assets = assetService.loadAssets(splitIds(), type); for (Asset asset : assets) { mergeProps(asset); } return SUCCESS; } /** * ??<br> * ?propskey?<br> * ?propskeyvalue==nullvalue?????null<br> * * @param asset */ private void mergeProps(Asset asset) { merge("name", asset.getName()); merge("vendor", asset.getVendor()); merge("modelOrVersion", asset.getModelOrVersion()); merge("assetUsage", asset.getAssetUsage()); merge("purchaseTime", asset.getPurchaseTime()); merge("quantity", asset.getQuantity()); merge("cost", asset.getCost()); merge("comment", asset.getComment()); if (asset instanceof Hardware) { merge("code", ((Hardware) asset).getCode()); merge("financialCode", ((Hardware) asset).getFinancialCode()); merge("sn", ((Hardware) asset).getSn()); merge("configuration", ((Hardware) asset).getConfiguration()); merge("warranty", ((Hardware) asset).getWarranty()); merge("location", ((Hardware) asset).getLocation()); merge("ip", ((Hardware) asset).getIp()); merge("importance", ((Hardware) asset).getImportance()); } else if (asset instanceof Software) { merge("softwareType", ((Software) asset).getSoftwareType()); merge("license", ((Software) asset).getLicense()); merge("expiredTime", ((Software) asset).getExpiredTime()); } } private void merge(String key, Object value) { if (null == value) { // null??purchaseTimeexpiredTime?null?? props.put(key, value); } else { if (props.containsKey(key)) { // ?propskey Object oldValue = props.get(key); if (null != oldValue && !value.equals(oldValue)) { // ?null?merge? // value??null? props.put(key, null); } // ??null????value???props?? } else { // propskey?? props.put(key, value); } } } private int result; public int getResult() { return result; } public void setResult(int result) { this.result = result; } /** * ????-99?<br> * MapString????????<br> * <ul> * ? * <li>?? * <li>?null * <li>-99???? * <li>? * </ul> * <ul> * * <li>????? * <li>????? * <li>-99????? * <li>????? * </ul> * * @return * @throws Exception */ @Action(value = "save-props", results = { @Result(type = "json", params = { "root", "result" }) }) public String saveProps() throws Exception { result = assetService.saveProps(splitIds(), type, transProps()); return SUCCESS; } /** * ???Map??String[]?null<br> * ??action?action<br> * ?????null * * @return * @throws Exception */ private Map<String, Object> transProps() throws Exception { Map<String, Object> p = new HashMap<String, Object>(); Set<String> keys = props.keySet(); for (String key : keys) { String value = ((String[]) props.get(key))[0]; if ("expiredTime".equals(key) || "purchaseTime".equals(key)) { p.put(key, "".equals(value) ? null : (new SimpleDateFormat("yyyy-MM-dd")).parse(value)); } else if ("quantity".equals(key) || "number".equals(key)) { p.put(key, Integer.parseInt(value)); } else if ("cost".equals(key)) { p.put(key, BigDecimal.valueOf(Double.parseDouble(value))); } else if ("warranty".equals(key) || "importance".equals(key) || "softwareType".equals(key) || "type".equals(key) || "catalog".equals(key) || "state".equals(key)) { p.put(key, Byte.parseByte(value)); } else if ("ownerId".equals(key)) { p.put("owner", (Employee) hrService.getUnit(Long.parseLong(value), HRUnit.EMPLOYEE)); } else { p.put(key, value); } } if (!p.containsKey("company")) { p.put("company", getCurrentCompany()); } return p; } private Map<String, String> validation; public Map<String, String> getValidation() { return validation; } public void setValidation(Map<String, String> validation) { this.validation = validation; } /** * ?? * * @return * @throws Exception */ @Action(value = "asset-vali", results = { @Result(type = "json", params = { "root", "validation" }) }) public String vali() throws Exception { validation = new LinkedHashMap<String, String>(); List<AssetValidateResult> v = assetService.validate(getCurrentCompany()); for (AssetValidateResult r : v) { StringBuilder builder = new StringBuilder("<li>"); switch (r.getResult()) { case AssetConstants.INVALID_OWNER: builder.append("").append(r.getAssetCatalogName()).append("<span>[") .append(r.getAssetFullName()).append("]</span>??"); break; case AssetConstants.IN_USE_ASSET_OWNED_BY_FORMER_EMPLOYEE: builder.append("").append(r.getAssetCatalogName()).append("<span>[") .append(r.getAssetFullName()) .append("]</span>?<span>[") .append(r.getOwnerName()).append("]</span>???"); break; case AssetConstants.INVALID_OWNER_COMPANY: builder.append("").append(r.getAssetCatalogName()).append("<span>[") .append(r.getAssetFullName()).append("]</span><span>[") .append(r.getOwnerName()).append("]</span><span>[").append(r.getOwnerCompanyName()) .append("]</span>??? <a href=\"javascript:void(0);\" class=\"_Chcmp\">?</a>"); break; case AssetConstants.IN_USE_ASSET_WITHOUT_OWNER: builder.append("").append(r.getAssetCatalogName()).append("<span>[") .append(r.getAssetFullName()) .append("]</span>??"); } builder.append( " <a href=\"javascript:void(0);\" class=\"_Chown\"></a> <a href=\"javascript:void(0);\" class=\"_SetIdle\"></a></li>"); validation.put(r.getId().toString(), builder.toString()); } return SUCCESS; } private Set<Byte> nextStates; public Set<Byte> getNextStates() { return nextStates; } public void setNextStates(Set<Byte> nextStates) { this.nextStates = nextStates; } /** * ???<br> * <ul> * * <li> -> / * <li> -> / * <li> -> null <br> * </ul> * <ul> * (???) * <li> -> // * <li> -> // * <li> -> //? * <li> -> ? * <li>? -> null <br> * </ul> * ??? * * @return * @throws Exception */ @Action(value = "check-state", results = { @Result(type = "json", params = { "root", "nextStates" }) }) public String checkState() throws Exception { nextStates = assetService.checkNextStates(splitIds(), type); return SUCCESS; } private byte state; // ? private long owner; // private boolean keep; // ??? public byte getState() { return state; } public void setState(byte state) { this.state = state; } public long getOwner() { return owner; } public void setOwner(long owner) { this.owner = owner; } public boolean isKeep() { return keep; } public void setKeep(boolean keep) { this.keep = keep; } /** * ? * * @return * @throws Exception */ @Action(value = "change-state", results = { @Result(type = "json", params = { "root", "result" }) }) public String changeState() throws Exception { result = assetService.changeState(splitIds(), type, state, (Employee) hrService.getUnit(owner, HRUnit.EMPLOYEE), keep); return SUCCESS; } /** * ???<br> * 0=1=2=?? */ private byte adjustType; public byte getAdjustType() { return adjustType; } public void setAdjustType(byte adjustType) { this.adjustType = adjustType; } /** * ??????id<br> * adjustType: 0=(???owner); 1=; 2=?? * * @return * @throws Exception */ @Action(value = "adjust", results = { @Result(type = "json", params = { "root", "result" }) }) public String adjust() throws Exception { result = assetService.adjust(id, adjustType, (Employee) hrService.getUnit(owner, HRUnit.EMPLOYEE)); return SUCCESS; } /** * ? * * @return * @throws Exception */ @Action(value = "new-assets", results = { @Result(type = "json", params = { "root", "result" }) }) public String newAsset() throws Exception { result = assetService.createNewAssets(transProps()); return SUCCESS; } private List<HardwareItem> owned; public List<HardwareItem> getOwned() { return owned; } public void setOwned(List<HardwareItem> owned) { this.owned = owned; } /** * ?? * * @return * @throws Exception */ @Action(value = "owned-equipments", results = { @Result(type = "json", params = { "root", "owned" }) }) public String ownedEquipments() throws Exception { List<Hardware> assets = assetService.loadEquipmentsByOwner(getCurrentEmployee()); owned = new ArrayList<HardwareItem>(); for (Hardware hw : assets) { owned.add(HardwareItem.createInstance(hw)); } return SUCCESS; } }