Roberto Sánchez
2014-01-08 02c2bc80c76440e849a73e9e7b58f1051f3cc47d
#394 feature - Added quick filter to listing
1 files added
5 files modified
changed files
securis/src/main/java/net/curisit/securis/db/LicenseType.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java patch | view | blame | history
securis/src/main/resources/static/admin.html patch | view | blame | history
securis/src/main/resources/static/js/admin.js patch | view | blame | history
securis/src/main/resources/static/js/catalogs.json patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/LicenseType.java
....@@ -5,6 +5,7 @@
55
66 import javax.persistence.Column;
77 import javax.persistence.Entity;
8
+import javax.persistence.GeneratedValue;
89 import javax.persistence.Id;
910 import javax.persistence.JoinColumn;
1011 import javax.persistence.ManyToOne;
....@@ -24,12 +25,13 @@
2425 @Entity
2526 @Table(name = "license_type")
2627 @NamedQueries(
27
- { @NamedQuery(name = "list-license_types", query = "SELECT a FROM Application a") })
28
+ { @NamedQuery(name = "list-license_types", query = "SELECT lt FROM LicenseType lt") })
2829 public class LicenseType implements Serializable {
2930
3031 private static final long serialVersionUID = 1L;
3132
3233 @Id
34
+ @GeneratedValue
3335 private int id;
3436
3537 private String code;
securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java
....@@ -3,6 +3,7 @@
33 import net.curisit.securis.services.ApplicationResource;
44 import net.curisit.securis.services.BasicServices;
55 import net.curisit.securis.services.LicenseServices;
6
+import net.curisit.securis.services.LicenseTypeResource;
67 import net.curisit.securis.services.SecurityInterceptor;
78 import net.curisit.securis.services.UserResource;
89
....@@ -22,9 +23,10 @@
2223 bind(BasicServices.class);
2324 bind(LicenseServices.class);
2425 bind(UserResource.class);
25
- bind(ApplicationResource.class);
2626 bind(SecurityInterceptor.class);
2727
28
+ bind(ApplicationResource.class);
29
+ bind(LicenseTypeResource.class);
2830 }
2931
3032 @Provides
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
....@@ -0,0 +1,150 @@
1
+package net.curisit.securis.services;
2
+
3
+import java.util.Date;
4
+import java.util.List;
5
+
6
+import javax.inject.Inject;
7
+import javax.inject.Provider;
8
+import javax.persistence.EntityManager;
9
+import javax.persistence.TypedQuery;
10
+import javax.servlet.http.HttpServletRequest;
11
+import javax.ws.rs.Consumes;
12
+import javax.ws.rs.DELETE;
13
+import javax.ws.rs.GET;
14
+import javax.ws.rs.HeaderParam;
15
+import javax.ws.rs.POST;
16
+import javax.ws.rs.PUT;
17
+import javax.ws.rs.Path;
18
+import javax.ws.rs.PathParam;
19
+import javax.ws.rs.Produces;
20
+import javax.ws.rs.core.Context;
21
+import javax.ws.rs.core.MediaType;
22
+import javax.ws.rs.core.Response;
23
+import javax.ws.rs.core.Response.Status;
24
+
25
+import net.curisit.integrity.commons.Utils;
26
+import net.curisit.securis.db.LicenseType;
27
+import net.curisit.securis.utils.TokenHelper;
28
+
29
+import org.slf4j.Logger;
30
+import org.slf4j.LoggerFactory;
31
+
32
+import com.google.inject.persist.Transactional;
33
+
34
+/**
35
+ * LicenseType resource, this service will provide methods to create, modify and delete license types
36
+ *
37
+ * @author roberto <roberto.sanchez@curisit.net>
38
+ */
39
+@Path("/licensetype")
40
+public class LicenseTypeResource {
41
+
42
+ @Inject
43
+ TokenHelper tokenHelper;
44
+
45
+ @Inject
46
+ Provider<EntityManager> emProvider;
47
+
48
+ private static final Logger log = LoggerFactory.getLogger(LicenseTypeResource.class);
49
+
50
+ public LicenseTypeResource() {
51
+ }
52
+
53
+ /**
54
+ *
55
+ * @return the server version in format majorVersion.minorVersion
56
+ */
57
+ @GET
58
+ @Path("/")
59
+ @Produces(
60
+ { MediaType.APPLICATION_JSON })
61
+ public Response index() {
62
+ log.info("Getting license types list ");
63
+
64
+ EntityManager em = emProvider.get();
65
+ TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
66
+ List<LicenseType> list = q.getResultList();
67
+
68
+ return Response.ok(list).build();
69
+ }
70
+
71
+ /**
72
+ *
73
+ * @return the server version in format majorVersion.minorVersion
74
+ */
75
+ @GET
76
+ @Path("/{ltid}")
77
+ @Produces(
78
+ { MediaType.APPLICATION_JSON })
79
+ public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
80
+ log.info("Getting license type data for id: {}: ", ltid);
81
+ if (ltid == null || ltid.equals("")) {
82
+ log.error("LicenseType ID is mandatory");
83
+ return Response.status(Status.NOT_FOUND).build();
84
+ }
85
+
86
+ EntityManager em = emProvider.get();
87
+ LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
88
+ if (lt == null) {
89
+ log.error("LicenseType with id {} not found in DB", ltid);
90
+ return Response.status(Status.NOT_FOUND).build();
91
+ }
92
+ return Response.ok(lt).build();
93
+ }
94
+
95
+ @POST
96
+ @Path("/")
97
+ @Consumes(MediaType.APPLICATION_JSON)
98
+ @Produces(
99
+ { MediaType.APPLICATION_JSON })
100
+ @Transactional
101
+ public Response create(LicenseType app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
102
+ log.info("Creating new license type");
103
+ EntityManager em = emProvider.get();
104
+ app.setCreationTimestamp(new Date());
105
+ em.persist(app);
106
+
107
+ return Response.ok(app).build();
108
+ }
109
+
110
+ @PUT
111
+ @POST
112
+ @Path("/{ltid}")
113
+ @Transactional
114
+ @Consumes(MediaType.APPLICATION_JSON)
115
+ @Produces(
116
+ { MediaType.APPLICATION_JSON })
117
+ public Response modify(LicenseType app, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
118
+ log.info("Modifying license type with id: {}", ltid);
119
+ EntityManager em = emProvider.get();
120
+ LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
121
+ if (currentlt == null) {
122
+ log.error("LicenseType with id {} not found in DB", ltid);
123
+ return Response.status(Status.NOT_FOUND).build();
124
+ }
125
+ currentlt.setName(app.getName());
126
+ currentlt.setDescription(app.getDescription());
127
+ em.persist(currentlt);
128
+
129
+ return Response.ok(currentlt).build();
130
+ }
131
+
132
+ @DELETE
133
+ @Path("/{ltid}")
134
+ @Transactional
135
+ @Produces(
136
+ { MediaType.APPLICATION_JSON })
137
+ public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
138
+ log.info("Deleting app with id: {}", ltid);
139
+ EntityManager em = emProvider.get();
140
+ LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid));
141
+ if (app == null) {
142
+ log.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
143
+ return Response.status(Status.NOT_FOUND).build();
144
+ }
145
+
146
+ em.remove(app);
147
+ return Response.ok(Utils.createMap("success", true, "id", ltid)).build();
148
+ }
149
+
150
+}
securis/src/main/resources/static/admin.html
....@@ -52,6 +52,8 @@
5252 margin-bottom: 5px;
5353 }
5454
55
+
56
+
5557 </style>
5658 <link rel="stylesheet"
5759 href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap-theme.min.css">
....@@ -109,15 +111,13 @@
109111 class="glyphicon glyphicon-ban-circle"></span> Cancel
110112 </a></li>
111113 </ul>
112
- <form class="navbar-form navbar-right" role="search">
113
- <div class="form-group">
114
- <input type="text" class="form-control" placeholder="Search" ng-model="searchText">
115
- </div>
116
- <button type="submit" class="btn btn-default">
117
- <span class="glyphicon glyphicon-search"></span>
118
-
119
- </button>
120
- </form>
114
+ <div class="navbar-form navbar-right">
115
+ <div class="input-group input-group-sm">
116
+ <span class="input-group-addon glyphicon glyphicon-search" style="top: 0px;"></span>
117
+ <input type="text" class="form-control" placeholder="Search" ng-model="$parent.searchText" >
118
+ <span class="btn input-group-addon glyphicon glyphicon-remove" ng-click="$parent.searchText = ''" style="top: 0px;"></span>
119
+ </div>
120
+ </div>
121121 </div>
122122 </nav>
123123 <div class="panel panel-default animate-show ng-hide" ng-show="showForm">
....@@ -126,12 +126,18 @@
126126 <div class="form-group" ng-repeat="field in catalogMetadata.fields" ng-if="!isNew || !field.readOnly">
127127 <label class="col-md-3 control-label" for="{{field.name}}">{{field.display}}</label>
128128 <div class="col-md-5">
129
- <input catalog-field ng-if="!field.readOnly && !field.multiline" type="{{field.type}}" id="{{field.name}}" name="{{field.name}}" placeholder=""
130
- class="form-control" ng-model="formu[field.name]" ng-required="field.mandatory" ng-maxlength="{{field.maxlength}}" />
131
- <textarea catalog-field ng-if="!field.readOnly && field.multiline" type="{{field.type}}" id="{{field.name}}" name="{{field.name}}" placeholder=""
132
- class="form-control" ng-model="formu[field.name]" rows="{{field.multiline}}" ng-required="field.mandatory" ng-maxlength="{{field.maxlength}}"></textarea>
133
- <p ng-if="field.readOnly" class="form-control-static">{{formu[field.name]}}</p>
134
-
129
+ <div ng-switch on="inputType(field)">
130
+ <input catalog-field ng-switch-when="normal" type="{{field.type}}" id="{{field.name}}" name="{{field.name}}" placeholder=""
131
+ class="form-control" ng-model="formu[field.name]" ng-required="field.mandatory" ng-maxlength="{{field.maxlength}}" />
132
+ <textarea catalog-field ng-switch-when="textarea" type="{{field.type}}" id="{{field.name}}" name="{{field.name}}" placeholder=""
133
+ class="form-control" ng-model="formu[field.name]" rows="{{field.multiline}}" ng-required="field.mandatory" ng-maxlength="{{field.maxlength}}"></textarea>
134
+ <p ng-switch-when="readonly" class="form-control-static">{{formu[field.name]}}</p>
135
+ <p ng-switch-when="readonly_date" class="form-control-static">{{formu[field.name] | date:'medium'}}</p>
136
+ <select ng-switch-when="select" class="form-control" ng-required="field.mandatory" ng-model="formu[field.name]"
137
+ ng-options="o.id as o.label for o in refs[field.name]">
138
+ </select>
139
+
140
+ </div>
135141 <div class="alert inline-alert alert-warning" ng-show="catalogForm[field.name].$invalid">
136142 <span class="glyphicon glyphicon-warning-sign"></span>
137143 <span ng-show="catalogForm[field.name].$error.maxlength">{{field.display}} length is too long (max: {{field.maxlength}}).<br/></span>
securis/src/main/resources/static/js/admin.js
....@@ -39,7 +39,7 @@
3939 method : "PUT"
4040 }
4141 }),
42
- licensetype : $resource('/licenseType/:licenseTypeId', {
42
+ licensetype : $resource('/licensetype/:licenseTypeId', {
4343 licenseTypeId : '@id'
4444 }, {
4545 update : {
....@@ -99,9 +99,9 @@
9999 }
100100 return resource.remove({}, data, success, fail)
101101 },
102
- query: function(catalog, callback) {
102
+ query: function(catalog) {
103103 console.log('HI catalog ???? ' + catalog);
104
- var resource = CatalogsService.resources[catalog.toLowerCase()];
104
+ var resource = CatalogsService.resources[catalog];
105105 function success(data) {
106106 console.log('success')
107107 console.log(data)
....@@ -124,6 +124,8 @@
124124 '$http',
125125 'Catalogs',
126126 function($scope, $http, Catalogs) {
127
+ $scope.showForm = true;
128
+ $scope.isNew = false;
127129 $scope.formu = {};
128130 $scope.catalogIndex = 0;
129131 $scope.catalogs = Catalogs.list(function() {
....@@ -141,11 +143,13 @@
141143 $scope.edit = function(data) {
142144 $scope.showForm = true;
143145 $scope.isNew = false;
146
+ $scope.formu = {}
144147 for (var k in data) {
145148 if (k.indexOf('$') !== 0) $scope.formu[k] = data[k]
146149 }
147
- // TODO: Load in formu values for Form
148
- // $scope.formu = {};
150
+ console.log('$scope.edit')
151
+ console.log($scope.formu)
152
+
149153 }
150154 $scope.delete = function(data) {
151155 BootstrapDialog.confirm('The record will be deleted, are you sure?', function(result){
....@@ -167,17 +171,32 @@
167171
168172 app.controller('CatalogFormCtrl', [ '$scope', '$http', 'toaster', 'Catalogs',
169173 function($scope, $http, toaster, Catalogs) {
170
- $scope.showForm = false;
171174 $scope.scope = $scope;
172175 console.log('Form: currentCatalog:' + $scope.cataLogIndex);
176
+
177
+ $scope.inputType = function(field) {
178
+
179
+ if (field.readOnly && field.type === 'date')
180
+ return 'readonly_date';
181
+ if (field.readOnly)
182
+ return 'readonly';
183
+ if (field.type === 'select')
184
+ return 'select';
185
+ if (!field.multiline)
186
+ return 'normal';
187
+ if (field.multiline)
188
+ return 'textarea';
173189
190
+ }
191
+
174192 $scope.editNew = function() {
175
- $scope.showForm = true;
176
- $scope.isNew = true;
177
- $scope.formu = {};
193
+ $('#name').focus();
194
+ $scope.$parent.showForm = true;
195
+ $scope.$parent.isNew = true;
196
+ $scope.$parent.formu = {};
178197 }
179198 $scope.cancel = function() {
180
- $scope.showForm = false;
199
+ $scope.$parent.showForm = false;
181200 }
182201
183202 $scope.saveCatalog = function() {
....@@ -188,12 +207,11 @@
188207 var promise = Catalogs.save(catalogName, $scope.formu).$promise;
189208 promise.then(function(data, otro) {
190209 if ($scope.isNew) {
191
- $scope.formu = {}
210
+ $scope.$parent.formu = {}
192211 $('#name').focus();
193212 } else {
194213 $scope.cancel();
195214 }
196
- // $scope.formu = {};
197215 $scope.$parent.list = Catalogs.query(catalogName);
198216 }, function(error, otro) {
199217 console.log('then error');
securis/src/main/resources/static/js/catalogs.json
....@@ -36,6 +36,12 @@
3636 "pk" : true,
3737 "readOnly" : true
3838 }, {
39
+ "name" : "code",
40
+ "display" : "Code",
41
+ "type" : "string",
42
+ "maxlength" : 10,
43
+ "mandatory" : true
44
+ }, {
3945 "name" : "name",
4046 "display" : "Name",
4147 "type" : "string",
....@@ -48,6 +54,11 @@
4854 "maxlength" : 500,
4955 "multiline" : 2
5056 }, {
57
+ "name" : "application",
58
+ "display" : "Application",
59
+ "type" : "select",
60
+ "readOnly" : true
61
+ }, {
5162 "name" : "creationTimestamp",
5263 "display" : "Creation date",
5364 "type" : "date",