securis/src/main/java/net/curisit/securis/db/LicenseType.java
.. .. @@ -5,6 +5,7 @@ 5 5 6 6 import javax.persistence.Column; 7 7 import javax.persistence.Entity; 8 +import javax.persistence.GeneratedValue;8 9 import javax.persistence.Id; 9 10 import javax.persistence.JoinColumn; 10 11 import javax.persistence.ManyToOne; .. .. @@ -24,12 +25,13 @@ 24 25 @Entity 25 26 @Table(name = "license_type") 26 27 @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") })28 29 public class LicenseType implements Serializable { 29 30 30 31 private static final long serialVersionUID = 1L; 31 32 32 33 @Id 34 + @GeneratedValue33 35 private int id; 34 36 35 37 private String code; securis/src/main/java/net/curisit/securis/ioc/RequestsModule.java
.. .. @@ -3,6 +3,7 @@ 3 3 import net.curisit.securis.services.ApplicationResource; 4 4 import net.curisit.securis.services.BasicServices; 5 5 import net.curisit.securis.services.LicenseServices; 6 +import net.curisit.securis.services.LicenseTypeResource;6 7 import net.curisit.securis.services.SecurityInterceptor; 7 8 import net.curisit.securis.services.UserResource; 8 9 .. .. @@ -22,9 +23,10 @@ 22 23 bind(BasicServices.class); 23 24 bind(LicenseServices.class); 24 25 bind(UserResource.class); 25 - bind(ApplicationResource.class);26 26 bind(SecurityInterceptor.class); 27 27 28 + bind(ApplicationResource.class);29 + bind(LicenseTypeResource.class);28 30 } 29 31 30 32 @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 types36 + *37 + * @author roberto <roberto.sanchez@curisit.net>38 + */39 +@Path("/licensetype")40 +public class LicenseTypeResource {41 +42 + @Inject43 + TokenHelper tokenHelper;44 +45 + @Inject46 + 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.minorVersion56 + */57 + @GET58 + @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.minorVersion74 + */75 + @GET76 + @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 + @POST96 + @Path("/")97 + @Consumes(MediaType.APPLICATION_JSON)98 + @Produces(99 + { MediaType.APPLICATION_JSON })100 + @Transactional101 + 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 + @PUT111 + @POST112 + @Path("/{ltid}")113 + @Transactional114 + @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 + @DELETE133 + @Path("/{ltid}")134 + @Transactional135 + @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 @@ 52 52 margin-bottom: 5px; 53 53 } 54 54 55 +56 +55 57 </style> 56 58 <link rel="stylesheet" 57 59 href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap-theme.min.css"> .. .. @@ -109,15 +111,13 @@ 109 111 class="glyphicon glyphicon-ban-circle"></span> Cancel 110 112 </a></li> 111 113 </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>121 121 </div> 122 122 </nav> 123 123 <div class="panel panel-default animate-show ng-hide" ng-show="showForm"> .. .. @@ -126,12 +126,18 @@ 126 126 <div class="form-group" ng-repeat="field in catalogMetadata.fields" ng-if="!isNew || !field.readOnly"> 127 127 <label class="col-md-3 control-label" for="{{field.name}}">{{field.display}}</label> 128 128 <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>135 141 <div class="alert inline-alert alert-warning" ng-show="catalogForm[field.name].$invalid"> 136 142 <span class="glyphicon glyphicon-warning-sign"></span> 137 143 <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 @@ 39 39 method : "PUT" 40 40 } 41 41 }), 42 - licensetype : $resource('/licenseType/:licenseTypeId', {42 + licensetype : $resource('/licensetype/:licenseTypeId', {43 43 licenseTypeId : '@id' 44 44 }, { 45 45 update : { .. .. @@ -99,9 +99,9 @@ 99 99 } 100 100 return resource.remove({}, data, success, fail) 101 101 }, 102 - query: function(catalog, callback) {102 + query: function(catalog) {103 103 console.log('HI catalog ???? ' + catalog); 104 - var resource = CatalogsService.resources[catalog.toLowerCase()];104 + var resource = CatalogsService.resources[catalog];105 105 function success(data) { 106 106 console.log('success') 107 107 console.log(data) .. .. @@ -124,6 +124,8 @@ 124 124 '$http', 125 125 'Catalogs', 126 126 function($scope, $http, Catalogs) { 127 + $scope.showForm = true;128 + $scope.isNew = false;127 129 $scope.formu = {}; 128 130 $scope.catalogIndex = 0; 129 131 $scope.catalogs = Catalogs.list(function() { .. .. @@ -141,11 +143,13 @@ 141 143 $scope.edit = function(data) { 142 144 $scope.showForm = true; 143 145 $scope.isNew = false; 146 + $scope.formu = {}144 147 for (var k in data) { 145 148 if (k.indexOf('$') !== 0) $scope.formu[k] = data[k] 146 149 } 147 - // TODO: Load in formu values for Form148 - // $scope.formu = {};150 + console.log('$scope.edit')151 + console.log($scope.formu)152 +149 153 } 150 154 $scope.delete = function(data) { 151 155 BootstrapDialog.confirm('The record will be deleted, are you sure?', function(result){ .. .. @@ -167,17 +171,32 @@ 167 171 168 172 app.controller('CatalogFormCtrl', [ '$scope', '$http', 'toaster', 'Catalogs', 169 173 function($scope, $http, toaster, Catalogs) { 170 - $scope.showForm = false;171 174 $scope.scope = $scope; 172 175 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';173 189 190 + }191 +174 192 $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 = {};178 197 } 179 198 $scope.cancel = function() { 180 - $scope.showForm = false;199 + $scope.$parent.showForm = false;181 200 } 182 201 183 202 $scope.saveCatalog = function() { .. .. @@ -188,12 +207,11 @@ 188 207 var promise = Catalogs.save(catalogName, $scope.formu).$promise; 189 208 promise.then(function(data, otro) { 190 209 if ($scope.isNew) { 191 - $scope.formu = {}210 + $scope.$parent.formu = {}192 211 $('#name').focus(); 193 212 } else { 194 213 $scope.cancel(); 195 214 } 196 - // $scope.formu = {};197 215 $scope.$parent.list = Catalogs.query(catalogName); 198 216 }, function(error, otro) { 199 217 console.log('then error'); securis/src/main/resources/static/js/catalogs.json
.. .. @@ -36,6 +36,12 @@ 36 36 "pk" : true, 37 37 "readOnly" : true 38 38 }, { 39 + "name" : "code",40 + "display" : "Code",41 + "type" : "string",42 + "maxlength" : 10,43 + "mandatory" : true44 + }, {39 45 "name" : "name", 40 46 "display" : "Name", 41 47 "type" : "string", .. .. @@ -48,6 +54,11 @@ 48 54 "maxlength" : 500, 49 55 "multiline" : 2 50 56 }, { 57 + "name" : "application",58 + "display" : "Application",59 + "type" : "select",60 + "readOnly" : true61 + }, {51 62 "name" : "creationTimestamp", 52 63 "display" : "Creation date", 53 64 "type" : "date",