Java tutorial
package com.jeans.iservlet.model.asset; import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; import java.util.Map; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.ForeignKey; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Index; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; import org.apache.commons.lang3.StringUtils; import org.apache.struts2.json.annotations.JSON; import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.NumericField; import com.jeans.iservlet.model.AssetConstants; import com.jeans.iservlet.model.hr.Company; import com.jeans.iservlet.model.hr.Employee; /** * ? * * : *?<br> * Asset: fullName:String(?name, vendor, modelOrVersion); comment:String<br> * Hardware: *sn:String, location:String, *ip:String, *code:String, *financialCode:String<br> * Software: *license<br> * company.id:long<br> * * @author Majorli * */ @Entity @Table(name = "assets", indexes = { @Index(name = "IDX_ASSET_TYPE", columnList = "type", unique = false), @Index(name = "IDX_ASSET_CATA", columnList = "catalog", unique = false), @Index(name = "IDX_ASSET_STATE", columnList = "state", unique = false) }) @Inheritance(strategy = InheritanceType.JOINED) @Indexed public class Asset implements Serializable { private long id; private Company company; private byte type; private byte catalog; private String name; private String vendor; private String modelOrVersion; private String assetUsage; private Date purchaseTime; private int quantity; private BigDecimal cost; private byte state; private String comment; public static Asset createAsset(byte type) { if (type == AssetConstants.HARDWARE_ASSET) { return new Hardware(); } else if (type == AssetConstants.SOFTWARE_ASSET) { return new Software(); } else { return null; } } public static Asset createAsset(Map<String, Object> properties) { byte type = (byte) properties.get("type"); Asset asset = Asset.createAsset(type); if (null != asset) { asset.setCompany((Company) properties.get("company")); asset.setType(type); asset.setCatalog((byte) properties.get("catalog")); asset.setName((String) properties.get("name")); asset.setVendor((String) properties.get("vendor")); asset.setModelOrVersion((String) properties.get("modelOrVersion")); asset.setAssetUsage((String) properties.get("assetUsage")); asset.setPurchaseTime((Date) properties.get("purchaseTime")); int quantity = (int) properties.get("quantity"); asset.setQuantity(quantity); asset.setCost((BigDecimal) properties.get("cost")); asset.setState((byte) properties.get("state")); asset.setComment((String) properties.get("comment")); if (asset instanceof Hardware) { ((Hardware) asset).setSn((String) properties.get("sn")); ((Hardware) asset).setConfiguration((String) properties.get("configuration")); ((Hardware) asset).setWarranty((byte) properties.get("warranty")); ((Hardware) asset).setLocation((String) properties.get("location")); ((Hardware) asset).setIp((String) properties.get("ip")); ((Hardware) asset).setImportance((byte) properties.get("importance")); ((Hardware) asset).setOwner((Employee) properties.get("owner")); String code = (String) properties.get("code"); if (quantity > 1) { code += "#1~" + quantity; } ((Hardware) asset).setCode(code); ((Hardware) asset).setFinancialCode((String) properties.get("financialCode")); } else if (asset instanceof Software) { ((Software) asset).setSoftwareType((byte) properties.get("softwareType")); ((Software) asset).setLicense((String) properties.get("license")); ((Software) asset).setExpiredTime((Date) properties.get("expiredTime")); } } return asset; } @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public long getId() { return id; } public void setId(long id) { this.id = id; } @ManyToOne @JoinColumn(nullable = true, name = "company_id", foreignKey = @ForeignKey(name = "FK_ASSET_COMP")) public Company getCompany() { return company; } public void setCompany(Company company) { this.company = company; } @Column(nullable = false) public byte getType() { return type; } public void setType(byte type) { this.type = type; } @Column(nullable = false) public byte getCatalog() { return catalog; } public void setCatalog(byte catalog) { this.catalog = catalog; } @Column(nullable = false, length = 32) public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(nullable = false, length = 32) public String getVendor() { return vendor; } public void setVendor(String vendor) { this.vendor = vendor; } @Column(nullable = false, name = "model_or_version", length = 64) public String getModelOrVersion() { return modelOrVersion; } public void setModelOrVersion(String modelOrVersion) { this.modelOrVersion = modelOrVersion; } @Column(nullable = false, name = "asset_usage", length = 255) public String getAssetUsage() { return assetUsage; } public void setAssetUsage(String assetUsage) { this.assetUsage = assetUsage; } @JSON(format = "yyyy-MM-dd HH:mm:ss") @Column(name = "purchase_time") public Date getPurchaseTime() { return purchaseTime; } public void setPurchaseTime(Date purchaseTime) { this.purchaseTime = purchaseTime; } @Column(nullable = false) public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } @Column(nullable = false) public BigDecimal getCost() { return cost; } public void setCost(BigDecimal cost) { this.cost = cost; } @Column(nullable = false) public byte getState() { return state; } public void setState(byte state) { this.state = state; } @Column(nullable = false, length = 255) @Field public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } @Transient @Field @NumericField public long getCompanyId() { return null == company ? 0 : company.getId(); } @Transient @Field public String getFullName() { if (StringUtils.isBlank(name)) { return ""; } else { StringBuilder fn = new StringBuilder(name); if (type == AssetConstants.SOFTWARE_ASSET) { if (!StringUtils.isBlank(vendor)) { fn.insert(0, " ").insert(0, vendor); } if (!StringUtils.isBlank(modelOrVersion)) { fn.append(" ").append(modelOrVersion); } } else { if (!StringUtils.isBlank(vendor)) { fn.append(" ").append(vendor); } if (!StringUtils.isBlank(modelOrVersion)) { fn.append(" ").append(modelOrVersion); } } return fn.toString(); } } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (int) (id ^ (id >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Asset other = (Asset) obj; if (id != other.id) return false; return true; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Asset [id=").append(id).append(", company=").append(company.getAlias()) .append(", fullName=").append(getFullName()).append("]"); return builder.toString(); } }