From c8eb07e8dc020346aaee0d859040ccabb79349bd Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Fri, 17 Jan 2014 12:26:05 +0000
Subject: [PATCH] #395 feature - Changes in REST API for license system

---
 securis/src/main/java/net/curisit/securis/db/Pack.java |   99 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 99 insertions(+), 0 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/db/Pack.java b/securis/src/main/java/net/curisit/securis/db/Pack.java
index 4c93f08..39e99ab 100644
--- a/securis/src/main/java/net/curisit/securis/db/Pack.java
+++ b/securis/src/main/java/net/curisit/securis/db/Pack.java
@@ -17,6 +17,8 @@
 import javax.persistence.Table;
 
 import org.codehaus.jackson.annotate.JsonAutoDetect;
+import org.codehaus.jackson.annotate.JsonIgnore;
+import org.codehaus.jackson.annotate.JsonProperty;
 import org.codehaus.jackson.map.annotate.JsonSerialize;
 
 /**
@@ -42,25 +44,34 @@
 	@Column(name = "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<License> licenses;
 
+	@JoinColumn(name = "num_licenses")
 	private int numLicenses;
 
 	public int getId() {
 		return id;
+	}
+
+	public void setId(int id) {
+		this.id = id;
 	}
 
 	public String getCode() {
@@ -111,4 +122,92 @@
 		this.numLicenses = numLicenses;
 	}
 
+	@JsonProperty("num_activations")
+	public int getNumActivations() {
+		if (licenses == null)
+			return 0;
+		int num = 0;
+		for (License lic : licenses) {
+			if (lic.getStatus() == License.Status.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() != License.Status.CANCELED)
+				num++;
+		}
+		return num;
+	}
+
+	/**
+	 * Number of available licenses in this pack
+	 * 
+	 * @return
+	 */
+	@JsonProperty("num_available")
+	public int getNumAvailables() {
+		return numLicenses - getNumCreations();
+	}
+
+	@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 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("licensetype_code")
+	public String getLicenseTypcode() {
+		return licenseType == null ? null : licenseType.getCode();
+	}
+
 }

--
Gitblit v1.3.2