piątek, 9 grudnia 2016

How to add user to new record in JHipster


When you have user relation to entity and need to add user to newly created record, follow this way.

Entity is i.e. Order.
In OrderResource.java add:


    @Inject
    private UserRepository UserRepository;



Somewhere at the end add:


 public String getCurrentUserLogin() {
        org.springframework.security.core.context.SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        String login = null;
        if (authentication != null)
            if (authentication.getPrincipal() instanceof UserDetails)
             login = ((UserDetails) authentication.getPrincipal()).getUsername();
            else if (authentication.getPrincipal() instanceof String)
             login = (String) authentication.getPrincipal();
        
        return login; 
        }





In the POST section, before *OrderRepository.save* add:



     User user=new User();
     user=UserRepository.findOneByLogin(getCurrentUserLogin()).get();
     Order.setUser(user);

wtorek, 6 grudnia 2016

How to create new custom view of entity in JHipster

I have normal entity productOnStock.
I need to have custom view for this entity for normal user.

To do this follow these steps.

  1. Create folder /src/main/webapp/app/manager/stock/
  2. Copy from /src/main/webapp/app/entities/product-on-stock/ files:
    1. product-on-stocks.html
    2. product-on-stock.state.js
    3. product-on-stock.service.js
    4. product-on-stock.search.service.js
    5. product-on-stock.controller.js
3. Paste them into  /src/main/webapp/app/manager/stock/
4. Change their names from product-on-stocks.* to stock.*
5. In stock.service.js:
  • change: .factory('ProductOnStock', ProductOnStock);
  • to: .factory(‘Stock', Stock);
  • -------------------------------
  • ProductOnStock.$inject = ['$resource', 'DateUtils'];
  • to: Stock.$inject = ['$resource', 'DateUtils'];
  • --------------------------------
  • function ProductOnStock ($resource, DateUtils) {
  •        var resourceUrl =  'api/product-on-stocks/:id';
  • to:
  • function Stock ($resource, DateUtils) {
  •        var resourceUrl =  'api/stock/:id';
6. In stock.state.js:
Change:


        .state('product-on-stock', {
            parent: 'entity',
            url: '/product-on-stock',
            data: {
                authorities: ['ROLE_USER'],
                pageTitle: 'barfitterApp.productOnStock.home.title'
            },
            views: {
                'content@': {
                    templateUrl: 'app/entities/product-on-stock/product-on-stocks.html',
                    controller: 'ProductOnStockController',
                    controllerAs: 'vm'
                }
            },
            resolve: {
                translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
                    $translatePartialLoader.addPart('productOnStock');
                    $translatePartialLoader.addPart('global');
                    return $translate.refresh();
                }]
            }
        })


to:


        .state('stock', {
            parent: 'manager',
            url: '/stock',
            data: {
                authorities: ['ROLE_MANAGER'],
                pageTitle: 'barfitterApp.productOnStock.home.title'
            },
            views: {
                'content@': {
                    templateUrl: 'app/manager/stock/stock.html',
                    controller: 'StockController',
                    controllerAs: 'vm'
                }
            },
            resolve: {
                translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
                    $translatePartialLoader.addPart('productOnStock');
                    $translatePartialLoader.addPart('global');
                    return $translate.refresh();
                }]
            }
        })


Comment other states for now.



7. In stock.controller.js

change:


        .controller('ProductOnStockController', ProductOnStockController);


    ProductOnStockController.$inject = ['$scope', '$state', 'ProductOnStock', 'ProductOnStockSearch'];


    function ProductOnStockController ($scope, $state, ProductOnStock, ProductOnStockSearch) {

to:

        .controller('StockController', StockController);


    StockController.$inject = ['$scope', '$state', 'ProductOnStock', 'ProductOnStockSearch', 'Stock'];


    function StockController ($scope, $state, ProductOnStock, ProductOnStockSearch, Stock) {

----------------

change:

        function loadAll() {
            ProductOnStock.query(function(result) {

to:

        function loadAll() {
            Stock.query(function(result) {



8. In stock.search.service.js change:



        .factory('ProductOnStockSearch', ProductOnStockSearch);

    ProductOnStockSearch.$inject = ['$resource'];

    function ProductOnStockSearch($resource) {
        var resourceUrl =  'api/_search/product-on-stocks/:id';

to:


        .factory('StockSearch', StockSearch);

    StockSearch.$inject = ['$resource'];

    function StockSearch($resource) {
        var resourceUrl =  'api/_search/stock/:id';