package net.curisit.securis.db; import java.io.Serializable; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.Table; import net.curisit.integrity.commons.Utils; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; /** * Entity implementation class for Entity: pack * */ @JsonAutoDetect @JsonInclude(Include.NON_NULL) @Entity @Table(name = "pack") @JsonIgnoreProperties(ignoreUnknown = true) @NamedQueries({ @NamedQuery(name = "list-packs", query = "SELECT pa FROM Pack pa"),// @NamedQuery(name = "list-packs-by-orgs", query = "SELECT pa FROM Pack pa where pa.organization.id in :list_ids") }) public class Pack implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private Integer id; private String code; private String comments; @Column(name = "creation_timestamp") @JsonProperty("creation_timestamp") private Date creationTimestamp; @JsonIgnore @ManyToOne @JoinColumn(name = "organization_id") private Organization organization; @JsonIgnore @ManyToOne @JoinColumn(name = "license_type_id") private LicenseType licenseType; @JsonIgnore @ManyToOne @JoinColumn(name = "created_by") private User createdBy; @JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pack") private Set licenses; @Column(name = "num_licenses") @JsonProperty("num_licenses") private int numLicenses; @Column(name = "init_valid_date") @JsonProperty("init_valid_date") private Date initValidDate; @Column(name = "end_valid_date") @JsonProperty("end_valid_date") private Date endValidDate; private PackStatus status; @Column(name = "license_preactivation") @JsonProperty("license_preactivation") private boolean licensePreactivation; @OneToMany(fetch = FetchType.LAZY, mappedBy = "pack") private Set metadata; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public Date getCreationTimestamp() { return creationTimestamp; } public void setCreationTimestamp(Date creationTimestamp) { this.creationTimestamp = creationTimestamp; } public Organization getOrganization() { return organization; } public void setOrganization(Organization organization) { this.organization = organization; } public LicenseType getLicenseType() { return licenseType; } public void setLicenseType(LicenseType licenseType) { this.licenseType = licenseType; } public User getCreatedBy() { return createdBy; } public void setCreatedBy(User createdBy) { this.createdBy = createdBy; } public int getNumLicenses() { return numLicenses; } public void setNumLicenses(int numLicenses) { this.numLicenses = numLicenses; } @JsonProperty("num_activations") public int getNumActivations() { if (licenses == null) { return 0; } int num = 0; for (License lic : licenses) { if (lic.getStatus() == LicenseStatus.ACTIVE || lic.getStatus() == LicenseStatus.PRE_ACTIVE) { num++; } } return num; } /** * Counts all created licenses, It counts active licenses and licenses * waiting for activation This number will be used to control the max number * of licenses created. Ignore canceled licenses. * * @return */ @JsonProperty("num_creations") public int getNumCreations() { if (licenses == null) { return 0; } int num = 0; for (License lic : licenses) { if (lic.getStatus() != LicenseStatus.CANCELLED) { num++; } } return num; } /** * Number of available licenses in this pack * * @return */ @JsonProperty("num_available") public int getNumAvailables() { return numLicenses - getNumActivations(); } @JsonProperty("organization_name") public String getOrgName() { return organization == null ? null : organization.getName(); } @JsonProperty("application_name") public String getAppName() { if (licenseType == null) { return null; } Application app = licenseType.getApplication(); return app == null ? null : app.getName(); } @JsonProperty("organization_id") public Integer getOrgId() { return organization == null ? null : organization.getId(); } @JsonProperty("organization_id") public void setOrgId(Integer idOrg) { if (idOrg == null) { organization = null; } else { organization = new Organization(); organization.setId(idOrg); } } @JsonProperty("license_type_id") public void setLicTypeId(Integer idLT) { if (idLT == null) { licenseType = null; } else { licenseType = new LicenseType(); licenseType.setId(idLT); } } @JsonProperty("license_type_id") public Integer getLicTypeId() { return licenseType == null ? null : licenseType.getId(); } @JsonProperty("created_by_id") public String getCreatedById() { return createdBy == null ? null : createdBy.getUsername(); } @JsonProperty("created_by_id") public void setCreatedById(String username) { createdBy = new User(); createdBy.setUsername(username); } @JsonProperty("created_by_name") public String getCreatedByname() { return createdBy == null ? null : String.format("%s %s (%s)", createdBy.getFirstName(), createdBy.getLastName() != null ? createdBy.getLastName() : "", createdBy.getUsername()); } @JsonProperty("licensetype_code") public String getLicenseTypeCode() { return licenseType == null ? null : licenseType.getCode(); } public String getComments() { return comments; } public void setComments(String comments) { this.comments = comments; } public boolean isLicensePreactivation() { return licensePreactivation; } public void setLicensePreactivation(boolean licensePreactivation) { this.licensePreactivation = licensePreactivation; } public Set getMetadata() { return metadata; } public void setMetadata(Set metadata) { this.metadata = metadata; } public PackStatus getStatus() { return status; } public void setStatus(PackStatus status) { this.status = status; } public Date getInitValidDate() { return initValidDate; } public void setInitValidDate(Date initValidDate) { this.initValidDate = initValidDate; } public Date getEndValidDate() { return endValidDate; } public void setEndValidDate(Date endValidDate) { this.endValidDate = endValidDate; } @Override public boolean equals(Object obj) { if (!(obj instanceof Pack)) return false; Pack other = (Pack) obj; return id.equals(other.id); } @Override public int hashCode() { return (id == null ? 0 : id.hashCode()); } public static class Action { public static final int CREATE = 1; public static final int ACTIVATION = 2; public static final int PUT_ONHOLD = 3; public static final int CANCEL = 4; public static final int DELETE = 5; } public static class Status { private static final Map> transitions = Utils.createMap( // Action.ACTIVATION, Arrays.asList(PackStatus.CREATED, PackStatus.ON_HOLD, PackStatus.EXPIRED), // Action.PUT_ONHOLD, Arrays.asList(PackStatus.ACTIVE), // Action.CANCEL, Arrays.asList(PackStatus.ACTIVE, PackStatus.ON_HOLD, PackStatus.EXPIRED), // Action.DELETE, Arrays.asList(PackStatus.CANCELLED, PackStatus.CREATED) // ); /** * It checks if a given action is valid for the License, passing the * action and the current license status * * @param oldStatus * @param newStatus * @return */ public static boolean isActionValid(Integer action, PackStatus currentStatus) { List validStatuses = transitions.get(currentStatus); return validStatuses != null && validStatuses.contains(currentStatus); } } }