From 02c2bc80c76440e849a73e9e7b58f1051f3cc47d Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Wed, 08 Jan 2014 18:11:31 +0000
Subject: [PATCH] #394 feature - Added quick filter to listing

---
 securis/src/main/resources/static/admin.html                                |   36 ++++---
 securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java |  150 ++++++++++++++++++++++++++++++
 securis/src/main/resources/static/js/catalogs.json                          |   11 ++
 securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java           |    4 
 securis/src/main/java/net/curisit/securis/db/LicenseType.java               |    4 
 securis/src/main/resources/static/js/admin.js                               |   42 ++++++--
 6 files changed, 218 insertions(+), 29 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/db/LicenseType.java b/securis/src/main/java/net/curisit/securis/db/LicenseType.java
index 771fb54..675f108 100644
--- a/securis/src/main/java/net/curisit/securis/db/LicenseType.java
+++ b/securis/src/main/java/net/curisit/securis/db/LicenseType.java
@@ -5,6 +5,7 @@
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
@@ -24,12 +25,13 @@
 @Entity
 @Table(name = "license_type")
 @NamedQueries(
-	{ @NamedQuery(name = "list-license_types", query = "SELECT a FROM Application a") })
+	{ @NamedQuery(name = "list-license_types", query = "SELECT lt FROM LicenseType lt") })
 public class LicenseType implements Serializable {
 
 	private static final long serialVersionUID = 1L;
 
 	@Id
+	@GeneratedValue
 	private int id;
 
 	private String code;
diff --git a/securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java b/securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java
index 03424d5..2e2eacf 100644
--- a/securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java
+++ b/securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java
@@ -3,6 +3,7 @@
 import net.curisit.securis.services.ApplicationResource;
 import net.curisit.securis.services.BasicServices;
 import net.curisit.securis.services.LicenseServices;
+import net.curisit.securis.services.LicenseTypeResource;
 import net.curisit.securis.services.SecurityInterceptor;
 import net.curisit.securis.services.UserResource;
 
@@ -22,9 +23,10 @@
 		bind(BasicServices.class);
 		bind(LicenseServices.class);
 		bind(UserResource.class);
-		bind(ApplicationResource.class);
 		bind(SecurityInterceptor.class);
 
+		bind(ApplicationResource.class);
+		bind(LicenseTypeResource.class);
 	}
 
 	@Provides
diff --git a/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java b/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
new file mode 100644
index 0000000..87b1c37
--- /dev/null
+++ b/securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
@@ -0,0 +1,150 @@
+package net.curisit.securis.services;
+
+import java.util.Date;
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.inject.Provider;
+import javax.persistence.EntityManager;
+import javax.persistence.TypedQuery;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+
+import net.curisit.integrity.commons.Utils;
+import net.curisit.securis.db.LicenseType;
+import net.curisit.securis.utils.TokenHelper;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.persist.Transactional;
+
+/**
+ * LicenseType resource, this service will provide methods to create, modify and delete license types
+ * 
+ * @author roberto <roberto.sanchez@curisit.net>
+ */
+@Path("/licensetype")
+public class LicenseTypeResource {
+
+	@Inject
+	TokenHelper tokenHelper;
+
+	@Inject
+	Provider<EntityManager> emProvider;
+
+	private static final Logger log = LoggerFactory.getLogger(LicenseTypeResource.class);
+
+	public LicenseTypeResource() {
+	}
+
+	/**
+	 * 
+	 * @return the server version in format majorVersion.minorVersion
+	 */
+	@GET
+	@Path("/")
+	@Produces(
+		{ MediaType.APPLICATION_JSON })
+	public Response index() {
+		log.info("Getting license types list ");
+
+		EntityManager em = emProvider.get();
+		TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
+		List<LicenseType> list = q.getResultList();
+
+		return Response.ok(list).build();
+	}
+
+	/**
+	 * 
+	 * @return the server version in format majorVersion.minorVersion
+	 */
+	@GET
+	@Path("/{ltid}")
+	@Produces(
+		{ MediaType.APPLICATION_JSON })
+	public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
+		log.info("Getting license type data for id: {}: ", ltid);
+		if (ltid == null || ltid.equals("")) {
+			log.error("LicenseType ID is mandatory");
+			return Response.status(Status.NOT_FOUND).build();
+		}
+
+		EntityManager em = emProvider.get();
+		LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
+		if (lt == null) {
+			log.error("LicenseType with id {} not found in DB", ltid);
+			return Response.status(Status.NOT_FOUND).build();
+		}
+		return Response.ok(lt).build();
+	}
+
+	@POST
+	@Path("/")
+	@Consumes(MediaType.APPLICATION_JSON)
+	@Produces(
+		{ MediaType.APPLICATION_JSON })
+	@Transactional
+	public Response create(LicenseType app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
+		log.info("Creating new license type");
+		EntityManager em = emProvider.get();
+		app.setCreationTimestamp(new Date());
+		em.persist(app);
+
+		return Response.ok(app).build();
+	}
+
+	@PUT
+	@POST
+	@Path("/{ltid}")
+	@Transactional
+	@Consumes(MediaType.APPLICATION_JSON)
+	@Produces(
+		{ MediaType.APPLICATION_JSON })
+	public Response modify(LicenseType app, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
+		log.info("Modifying license type with id: {}", ltid);
+		EntityManager em = emProvider.get();
+		LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
+		if (currentlt == null) {
+			log.error("LicenseType with id {} not found in DB", ltid);
+			return Response.status(Status.NOT_FOUND).build();
+		}
+		currentlt.setName(app.getName());
+		currentlt.setDescription(app.getDescription());
+		em.persist(currentlt);
+
+		return Response.ok(currentlt).build();
+	}
+
+	@DELETE
+	@Path("/{ltid}")
+	@Transactional
+	@Produces(
+		{ MediaType.APPLICATION_JSON })
+	public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
+		log.info("Deleting app with id: {}", ltid);
+		EntityManager em = emProvider.get();
+		LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid));
+		if (app == null) {
+			log.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
+			return Response.status(Status.NOT_FOUND).build();
+		}
+
+		em.remove(app);
+		return Response.ok(Utils.createMap("success", true, "id", ltid)).build();
+	}
+
+}
diff --git a/securis/src/main/resources/static/admin.html b/securis/src/main/resources/static/admin.html
index 5c6f344..07a95a3 100644
--- a/securis/src/main/resources/static/admin.html
+++ b/securis/src/main/resources/static/admin.html
@@ -52,6 +52,8 @@
 	margin-bottom: 5px;
 }
 
+
+
 </style>
 <link rel="stylesheet"
 	href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap-theme.min.css">
@@ -109,15 +111,13 @@
 									class="glyphicon glyphicon-ban-circle"></span> Cancel
 							</a></li>
 						</ul>
-						<form class="navbar-form navbar-right" role="search">
-							<div class="form-group">
-								<input type="text" class="form-control" placeholder="Search" ng-model="searchText">
-							</div>
-							<button type="submit" class="btn btn-default">
-								<span class="glyphicon glyphicon-search"></span>
-
-							</button>
-						</form>
+						<div class="navbar-form navbar-right">
+						<div class="input-group input-group-sm">
+						  	<span class="input-group-addon glyphicon glyphicon-search" style="top: 0px;"></span>
+						  <input type="text" class="form-control" placeholder="Search" ng-model="$parent.searchText" >
+								<span class="btn input-group-addon glyphicon glyphicon-remove" ng-click="$parent.searchText = ''" style="top: 0px;"></span>
+						</div>
+						</div>
 					</div>
 				</nav>
 				<div class="panel panel-default animate-show ng-hide" ng-show="showForm">
@@ -126,12 +126,18 @@
 						<div class="form-group" ng-repeat="field in catalogMetadata.fields" ng-if="!isNew || !field.readOnly">
 							<label class="col-md-3 control-label" for="{{field.name}}">{{field.display}}</label>
 							<div class="col-md-5">
-								<input catalog-field ng-if="!field.readOnly && !field.multiline" type="{{field.type}}" id="{{field.name}}" name="{{field.name}}" placeholder=""
-									class="form-control" ng-model="formu[field.name]" ng-required="field.mandatory" ng-maxlength="{{field.maxlength}}" />
-								<textarea catalog-field ng-if="!field.readOnly && field.multiline" type="{{field.type}}" id="{{field.name}}" name="{{field.name}}" placeholder=""
-									class="form-control" ng-model="formu[field.name]" rows="{{field.multiline}}" ng-required="field.mandatory" ng-maxlength="{{field.maxlength}}"></textarea>
-								<p ng-if="field.readOnly" class="form-control-static">{{formu[field.name]}}</p>
-								
+								<div ng-switch on="inputType(field)">
+									<input catalog-field ng-switch-when="normal" type="{{field.type}}" id="{{field.name}}" name="{{field.name}}" placeholder=""
+										class="form-control" ng-model="formu[field.name]" ng-required="field.mandatory" ng-maxlength="{{field.maxlength}}" />
+									<textarea catalog-field ng-switch-when="textarea" type="{{field.type}}" id="{{field.name}}" name="{{field.name}}" placeholder=""
+										class="form-control" ng-model="formu[field.name]" rows="{{field.multiline}}" ng-required="field.mandatory" ng-maxlength="{{field.maxlength}}"></textarea>
+									<p ng-switch-when="readonly" class="form-control-static">{{formu[field.name]}}</p>
+									<p ng-switch-when="readonly_date" class="form-control-static">{{formu[field.name] | date:'medium'}}</p>
+									<select ng-switch-when="select" class="form-control" ng-required="field.mandatory" ng-model="formu[field.name]"
+										ng-options="o.id as o.label for o in refs[field.name]">
+									</select>
+									
+								</div>
 								<div class="alert inline-alert alert-warning" ng-show="catalogForm[field.name].$invalid">
 								    <span class="glyphicon glyphicon-warning-sign"></span>
 								    <span ng-show="catalogForm[field.name].$error.maxlength">{{field.display}} length is too long (max: {{field.maxlength}}).<br/></span>
diff --git a/securis/src/main/resources/static/js/admin.js b/securis/src/main/resources/static/js/admin.js
index 7bd2645..6b4ecec 100644
--- a/securis/src/main/resources/static/js/admin.js
+++ b/securis/src/main/resources/static/js/admin.js
@@ -39,7 +39,7 @@
 						method : "PUT"
 					}
 				}),
-				licensetype : $resource('/licenseType/:licenseTypeId', {
+				licensetype : $resource('/licensetype/:licenseTypeId', {
 					licenseTypeId : '@id'
 				}, {
 					update : {
@@ -99,9 +99,9 @@
 				}
 				return resource.remove({}, data, success, fail)
 			},
-			query: function(catalog, callback) {
+			query: function(catalog) {
 				console.log('HI catalog ???? ' + catalog);
-				var resource = CatalogsService.resources[catalog.toLowerCase()];
+				var resource = CatalogsService.resources[catalog];
 				function success(data) {
 					console.log('success')
 					console.log(data)
@@ -124,6 +124,8 @@
 			'$http',
 			'Catalogs',
 			function($scope, $http, Catalogs) {
+				$scope.showForm = true;
+				$scope.isNew = false;
 				$scope.formu = {};
 				$scope.catalogIndex = 0;
 				$scope.catalogs = Catalogs.list(function() {
@@ -141,11 +143,13 @@
 				$scope.edit = function(data) {
 					$scope.showForm = true;
 					$scope.isNew = false;
+					$scope.formu = {}
 					for (var k in data) {
 						if (k.indexOf('$') !== 0) $scope.formu[k] = data[k]
 					}
-					// TODO: Load in formu values for Form
-					// $scope.formu = {};
+					console.log('$scope.edit')
+					console.log($scope.formu)
+					
 				}
 				$scope.delete = function(data) {
 					BootstrapDialog.confirm('The record will be deleted, are you sure?', function(result){
@@ -167,17 +171,32 @@
 
 	app.controller('CatalogFormCtrl', [ '$scope', '$http', 'toaster', 'Catalogs',
 			function($scope, $http, toaster, Catalogs) {
-				$scope.showForm = false;
 				$scope.scope = $scope;
 				console.log('Form: currentCatalog:' + $scope.cataLogIndex);
+				
+				$scope.inputType = function(field) {
+					
+					if (field.readOnly && field.type === 'date')
+						return 'readonly_date';
+					if (field.readOnly)
+						return 'readonly';
+					if (field.type === 'select')
+						return 'select';
+					if (!field.multiline)
+						return 'normal';
+					if (field.multiline)
+						return 'textarea';
 
+				}
+				
 				$scope.editNew = function() {
-					$scope.showForm = true;
-					$scope.isNew = true;
-					$scope.formu = {};
+					$('#name').focus();
+					$scope.$parent.showForm = true;
+					$scope.$parent.isNew = true;
+					$scope.$parent.formu = {};
 				}
 				$scope.cancel = function() {
-					$scope.showForm = false;
+					$scope.$parent.showForm = false;
 				}
 
 				$scope.saveCatalog = function() {
@@ -188,12 +207,11 @@
 						var promise = Catalogs.save(catalogName, $scope.formu).$promise;
 						promise.then(function(data, otro) {
 							if ($scope.isNew) {
-								$scope.formu = {}
+								$scope.$parent.formu = {}
 								$('#name').focus();
 							} else {
 								$scope.cancel();
 							}
-						//	$scope.formu = {};
 							$scope.$parent.list = Catalogs.query(catalogName);
 						}, function(error, otro) {
 							console.log('then error');
diff --git a/securis/src/main/resources/static/js/catalogs.json b/securis/src/main/resources/static/js/catalogs.json
index 33e2983..f123dac 100644
--- a/securis/src/main/resources/static/js/catalogs.json
+++ b/securis/src/main/resources/static/js/catalogs.json
@@ -36,6 +36,12 @@
 		"pk" : true,
 		"readOnly" : true
 	}, {
+		"name" : "code",
+		"display" : "Code",
+		"type" : "string",
+		"maxlength" : 10,
+		"mandatory" : true
+	}, {
 		"name" : "name",
 		"display" : "Name",
 		"type" : "string",
@@ -48,6 +54,11 @@
 		"maxlength" : 500,
 		"multiline" : 2
 	}, {
+		"name" : "application",
+		"display" : "Application",
+		"type" : "select",
+		"readOnly" : true
+	}, {
 		"name" : "creationTimestamp",
 		"display" : "Creation date",
 		"type" : "date",

--
Gitblit v1.3.2