wtorek, 29 listopada 2016

How to create new ROLE in JHipster


Let’s assume you want to add new role ROLE_MANAGER.

1. In /src/main/resources/config/liquibase/authorities.csv
add ROLE_MANAGER in new line.


In AuthoritiesConstants.java add:

public static final String MANAGER = "ROLE_MANAGER";



2. In /src/main/webapp/app/admin/user-management/user-management.controller.js and user-management-dialog.controller.js
add new role:
vm.authorities = ['ROLE_USER', 'ROLE_ADMIN', 'ROLE_MANAGER'];

3. In
/src/main/webapp/app/account/password/password.state.js
/src/main/webapp/app/account/sessions/sessions.state.js
/src/main/webapp/app/account/settings/settings.state.js

add
authorities: ['ROLE_USER', 'ROLE_MANAGER'],

4. In table jhi_authority
add ROLE_MANAGER


5. In console run:
./mvnw liquibase:clearCheckSums

poniedziałek, 14 listopada 2016

How to delete multiple records in Spring Boot and Jhipster

I have authorization.html with records listing as it’s generated in JHipster.
Each record has it’s own delete button, but if I need to delete many of them, it’s more convenient to have checkboxes assigned to each record and one button to delete selected records.
My entity is productDelivered not authorization.

In html file, I added checkboxes:


<th><input type="checkbox" ng-click="vm.allNeedsClicked()" ng-checked="vm.allNeedsMet()"></th>
...
<tbody>
    <tr
     ng-repeat="productDelivered in vm.authorizations track by productDelivered.id">
     <td><input type='checkbox'  ng-model="productDelivered.checked"></td>
     <td><a
      ui-sref="product-delivered-detail({id:productDelivered.id})">{{productDelivered.id}}</a></td>

[ng-click="vm.allNeedsClicked()" ng-checked="vm.allNeedsMet()" are functions for selecting all checkboxes]

and below </table> added:


<br>
 <button ng-click="vm.deleteSelectedItems()"
  class="pull-left btn btn-danger" type="button" title="Delete">
  Delete selected <span class="glyphicon glyphicon-trash"></span>
 </button>


In authorization.controller.js added:

       function deleteSelectedItems() { // pressed button in the bottom
         Authorization.update({id: 7});
         var deletedIndex=[];
   angular.forEach(vm.authorizations, function(productDelivered) {
    if (productDelivered.checked) {
     deletedIndex.push(vm.authorizations.indexOf(productDelivered));          
     ProductDelivered.delete({id: productDelivered.id});
    }
   });
   // splice usuwa element tablicy przez co zmienia indeksację powodując że usuwany jest tylko co drugi element, dlatego przed tym tablica jest odwracana
   deletedIndex.reverse(); 
   deletedIndex.forEach(function(elt) {
    vm.authorizations.splice(elt, 1);
   })
  }


  
  function allNeedsClicked() {
      var newValue = !vm.allNeedsMet();
        angular.forEach(vm.authorizations, function (productDelivered) {
        productDelivered.checked = newValue;
      });
    };
    
    // Returns true if and only if all todos are done.
    function allNeedsMet() {
      var needsMet = vm.authorizations.reduce(function (memo, productDelivered) {
        return memo + (productDelivered.checked ? 1 : 0);
      }, 0);
      return (needsMet === vm.authorizations.length);
    };

On the top we need also:

        vm.deleteSelectedItems=deleteSelectedItems;
        
        vm.allNeedsClicked=allNeedsClicked;
        vm.allNeedsMet=allNeedsMet;



Warning! It deletes records without any confirmation!



How to list two [or more] entities in one view in Jhipster

I created new menu entry like this:

On new page - authorization.html I want to list two entities generated by JHipster:
  • productDelivered
  • category

I created /src/main/webapp/app/authorization/ folder. I copied there files from /src/main/webapp/app/entities/product-delivered/:
  • product-delivereds.html
  • product-delivered.state.js
  • product-delivered.service.js
  • product-delivered.controller.js

and changed their names from product-delivered to authorization and some instances of product-delivered inside of these files as well.

In authorization.controller.js added Category:

(function() {
    'use strict';

    angular
        .module('barfitterApp')
        .controller('AuthorizationController', AuthorizationController);

    AuthorizationController.$inject = ['$scope', '$state', 'Authorization', 'ProductDelivered', 'Category', 'ProductDeliveredSearch', 'ParseLinks', 'AlertService', 'pagingParams', 'paginationConstants'];

    function AuthorizationController ($scope, $state, Authorization, ProductDelivered, Category, ProductDeliveredSearch, ParseLinks, AlertService, pagingParams, paginationConstants) {

and:


vm.categories = [];
...

            Category.query(function(result) {
                vm.categories = result;
            });



and in authorization.html added at the bottom content from category.html