From 89a0646d18da6f3290a883121e38f4086a6fb37e Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Wed, 07 Jun 2017 16:35:16 +0000
Subject: [PATCH] #3531 fea - Added acces to packs from license type, organizatins and applications listing

---
 securis/src/main/webapp/src/app/resources/base.ts                        |    4 
 securis/src/main/webapp/src/app/listing/license.list.component.ts        |    5 +
 securis/src/main/webapp/src/app/listing/application.list.component.ts    |    4 
 securis/src/main/webapp/src/app/listing/organization.list.component.ts   |    7 +-
 securis/src/main/webapp/sql_update.sql                                   |    1 
 securis/src/main/java/net/curisit/securis/services/PackResource.java     |   51 ++++++++++++++---
 securis/src/main/webapp/src/app/listing/pack.list.component.ts           |   50 ++++++++++++++--
 securis/src/main/webapp/src/app/listing/application.list.component.html  |    3 +
 securis/src/main/webapp/src/app/resources/packs.ts                       |   16 +++++
 securis/src/main/webapp/src/app/listing/pack.list.component.html         |   12 ++++
 securis/src/main/webapp/src/app/forms/license.form.component.ts          |    3 +
 securis/src/main/webapp/src/app/listing/licensetype.list.component.html  |    3 +
 securis/src/main/webapp/src/app/listing/license.list.component.html      |    1 
 securis/src/main/webapp/src/app/listing/licensetype.list.component.ts    |    4 
 securis/src/main/webapp/src/app/forms/license.form.html                  |    3 
 securis/src/main/webapp/src/app/listing/organization.list.component.html |    3 +
 16 files changed, 141 insertions(+), 29 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/services/PackResource.java b/securis/src/main/java/net/curisit/securis/services/PackResource.java
index 6c4db5a..5868d73 100644
--- a/securis/src/main/java/net/curisit/securis/services/PackResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/PackResource.java
@@ -1,6 +1,7 @@
 package net.curisit.securis.services;
 
 import java.security.Principal;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
@@ -21,8 +22,10 @@
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.core.UriInfo;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -80,33 +83,63 @@
 	@Path("/")
 	@Securable
 	@Produces({ MediaType.APPLICATION_JSON })
-	public Response index(@Context BasicSecurityContext bsc) {
+	public Response index(@Context UriInfo uriInfo, @Context BasicSecurityContext bsc) {
 		LOG.info("Getting packs list ");
+		MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
 
 		// EntityManager em = emProvider.get();
 		em.clear();
 
+		TypedQuery<Pack> q = createQuery(queryParams, bsc);
+		if (q == null) {
+			return Response.ok().build();
+		}
+
+		List<Pack> list = q.getResultList();
+
+		return Response.ok(list).build();
+	}
+
+	private String generateWhereFromParams(boolean addWhere, MultivaluedMap<String, String> queryParams) {
+		List<String> conditions = new ArrayList<>();
+		if (queryParams.containsKey("organizationId")) {
+			conditions.add(String.format("pa.organization.id = %s", queryParams.getFirst("organizationId")));
+		}
+		if (queryParams.containsKey("applicationId")) {
+			conditions.add(String.format("pa.licenseType.application.id = %s", queryParams.getFirst("applicationId")));
+		}
+		if (queryParams.containsKey("licenseTypeId")) {
+			conditions.add(String.format("pa.licenseType.id = %s", queryParams.getFirst("licenseTypeId")));
+		}
+		String connector = addWhere ? " where " : " and ";
+		return (conditions.isEmpty() ? "" : connector) + String.join(" and ", conditions);
+	}
+
+	private TypedQuery<Pack> createQuery(MultivaluedMap<String, String> queryParams, BasicSecurityContext bsc) {
 		TypedQuery<Pack> q;
+		String hql = "SELECT pa FROM Pack pa";
 		if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
-			LOG.info("Getting all packs for user: " + bsc.getUserPrincipal());
-			q = em.createNamedQuery("list-packs", Pack.class);
+			hql += generateWhereFromParams(true, queryParams);
+			q = em.createQuery(hql, Pack.class);
 		} else {
 			if (bsc.getApplicationsIds() == null || bsc.getApplicationsIds().isEmpty()) {
-				return Response.ok().build();
+				return null;
 			}
 			if (bsc.getOrganizationsIds() == null || bsc.getOrganizationsIds().isEmpty()) {
-				q = em.createNamedQuery("list-packs-by-apps", Pack.class);
+				hql += " where pa.licenseType.application.id in :list_ids_app ";
 			} else {
-				q = em.createNamedQuery("list-packs-by-orgs-apps", Pack.class);
+				hql += " where pa.organization.id in :list_ids_org and pa.licenseType.application.id in :list_ids_app ";
+			}
+			hql += generateWhereFromParams(false, queryParams);
+			q = em.createQuery(hql, Pack.class);
+			if (hql.contains("list_ids_org")) {
 				q.setParameter("list_ids_org", bsc.getOrganizationsIds());
 			}
 			q.setParameter("list_ids_app", bsc.getApplicationsIds());
 			LOG.info("Getting packs from orgs: {} and apps: {}", bsc.getOrganizationsIds(), bsc.getApplicationsIds());
 		}
 
-		List<Pack> list = q.getResultList();
-
-		return Response.ok(list).build();
+		return q;
 	}
 
 	private Response generateErrorUnathorizedAccess(Pack pack, Principal user) {
diff --git a/securis/src/main/webapp/sql_update.sql b/securis/src/main/webapp/sql_update.sql
index cfed213..06f11ba 100644
--- a/securis/src/main/webapp/sql_update.sql
+++ b/securis/src/main/webapp/sql_update.sql
@@ -7,4 +7,5 @@
   application_id INT NOT NULL,  
   PRIMARY KEY (username, application_id));
   
+update user set roles = 128 where username = '_client';
   
\ No newline at end of file
diff --git a/securis/src/main/webapp/src/app/forms/license.form.component.ts b/securis/src/main/webapp/src/app/forms/license.form.component.ts
index b4dbdd1..f6bf27c 100644
--- a/securis/src/main/webapp/src/app/forms/license.form.component.ts
+++ b/securis/src/main/webapp/src/app/forms/license.form.component.ts
@@ -111,6 +111,9 @@
     this.router.navigate([`packs/${this.pack.id}/licenses`]);
   }
 
+  editPack(): void {
+    this.router.navigate([`packs/edit/${this.pack.id}`]);
+  }
 
   ngAfterViewInit(): void {
     this.init();
diff --git a/securis/src/main/webapp/src/app/forms/license.form.html b/securis/src/main/webapp/src/app/forms/license.form.html
index 8d5115b..9413a10 100644
--- a/securis/src/main/webapp/src/app/forms/license.form.html
+++ b/securis/src/main/webapp/src/app/forms/license.form.html
@@ -8,7 +8,8 @@
 		<button md-icon-button (click)="save()"><md-icon>save</md-icon></button>
 		<md-toolbar-row class="inner-padding" *ngIf="!!pack" >
 			<div>
-				<span i18n>Pack</span>: {{pack.code}}
+				<span i18n>Pack</span>: {{pack.code}}		
+				<button md-icon-button (click)="editPack()"><md-icon>edit</md-icon></button>
 			</div>
 			<div class="inner-padding" flex="70" style="margin-left: 10px;" layout-align="start center" layout="row">
 				<md-chip selected [mdTooltip]="$L.get('field.application_name')" color="primary">{{pack.application_name}} </md-chip>
diff --git a/securis/src/main/webapp/src/app/listing/application.list.component.html b/securis/src/main/webapp/src/app/listing/application.list.component.html
index 60d6f0b..c839d6f 100644
--- a/securis/src/main/webapp/src/app/listing/application.list.component.html
+++ b/securis/src/main/webapp/src/app/listing/application.list.component.html
@@ -26,6 +26,9 @@
       <ng-template tdDataTableTemplate="menu" let-row="row" let-index="index">
         <div layout="row" layout-align="end center">
           <button md-icon-button (click)="edit(row.id)" color="primary"><md-icon>edit</md-icon></button>
+          <button md-icon-button (click)="showRelatedPacks(row)" color="accent" [mdTooltip]="$L.get('Show related packs')">  
+            <md-icon>arrow_forward</md-icon>
+          </button>
         </div>
       </ng-template>
     </td-data-table>
diff --git a/securis/src/main/webapp/src/app/listing/application.list.component.ts b/securis/src/main/webapp/src/app/listing/application.list.component.ts
index 5ebeb39..e4d5469 100644
--- a/securis/src/main/webapp/src/app/listing/application.list.component.ts
+++ b/securis/src/main/webapp/src/app/listing/application.list.component.ts
@@ -69,8 +69,8 @@
   }
 
 
-  packAction(action: any) {
-    console.log(action.command);
+  showRelatedPacks(app : any) : void {
+    this.router.navigate(['packs/'], {queryParams: {applicationId: app.id, name: app.name}});
   }
 
   isActionAvailable(pack : any) : boolean {
diff --git a/securis/src/main/webapp/src/app/listing/license.list.component.html b/securis/src/main/webapp/src/app/listing/license.list.component.html
index ec20631..2906d88 100644
--- a/securis/src/main/webapp/src/app/listing/license.list.component.html
+++ b/securis/src/main/webapp/src/app/listing/license.list.component.html
@@ -5,6 +5,7 @@
           <md-icon>arrow_back</md-icon>
       </button>
         <span class="md-title" i18n>Licenses for pack</span>: {{pack?.code}}
+				<button md-icon-button (click)="editPack()"><md-icon>edit</md-icon></button>
     </span>
     <span class="push-left-sm" *ngIf="filteredItems < data.length">
         <span class="md-body-1">{{filteredItems}} of {{data.length}} packs filtered</span>
diff --git a/securis/src/main/webapp/src/app/listing/license.list.component.ts b/securis/src/main/webapp/src/app/listing/license.list.component.ts
index c184d64..e1d0a76 100644
--- a/securis/src/main/webapp/src/app/listing/license.list.component.ts
+++ b/securis/src/main/webapp/src/app/listing/license.list.component.ts
@@ -111,8 +111,13 @@
 
   goBack() : void {
     this.router.navigate([`packs`]);
+  } 
+
+  editPack(): void { 
+    this.router.navigate([`packs/edit/${this.pack.id}`]);
   }
 
+
   isLicenseExpired(lic: any): boolean {
     return lic.expiration_date < (new Date().getTime());
   }
diff --git a/securis/src/main/webapp/src/app/listing/licensetype.list.component.html b/securis/src/main/webapp/src/app/listing/licensetype.list.component.html
index 76d6695..6be68a0 100644
--- a/securis/src/main/webapp/src/app/listing/licensetype.list.component.html
+++ b/securis/src/main/webapp/src/app/listing/licensetype.list.component.html
@@ -26,6 +26,9 @@
       <ng-template tdDataTableTemplate="menu" let-row="row" let-index="index">
         <div layout="row" layout-align="end center">
           <button md-icon-button (click)="edit(row.id)" color="primary"><md-icon>edit</md-icon></button>
+          <button md-icon-button (click)="showRelatedPacks(row)" color="accent" [mdTooltip]="$L.get('Show related packs')">  
+            <md-icon>arrow_forward</md-icon>
+          </button>
         </div>
       </ng-template>
     </td-data-table>
diff --git a/securis/src/main/webapp/src/app/listing/licensetype.list.component.ts b/securis/src/main/webapp/src/app/listing/licensetype.list.component.ts
index 1240b98..17d51de 100644
--- a/securis/src/main/webapp/src/app/listing/licensetype.list.component.ts
+++ b/securis/src/main/webapp/src/app/listing/licensetype.list.component.ts
@@ -66,8 +66,8 @@
   }
 
 
-  packAction(action: any) {
-    console.log(action.command);
+  showRelatedPacks(lt : any) : void {
+    this.router.navigate(['packs/'], {queryParams: {licenseTypeId: lt.id, name: lt.name}});
   }
 
   isActionAvailable(pack : any) : boolean {
diff --git a/securis/src/main/webapp/src/app/listing/organization.list.component.html b/securis/src/main/webapp/src/app/listing/organization.list.component.html
index ea2d0aa..fc528bc 100644
--- a/securis/src/main/webapp/src/app/listing/organization.list.component.html
+++ b/securis/src/main/webapp/src/app/listing/organization.list.component.html
@@ -26,6 +26,9 @@
       <ng-template tdDataTableTemplate="menu" let-row="row" let-index="index">
         <div layout="row" layout-align="end center">
           <button md-icon-button (click)="edit(row.id)" color="primary"><md-icon>edit</md-icon></button>
+          <button md-icon-button (click)="showRelatedPacks(row)" color="accent" [mdTooltip]="$L.get('Show related packs')">  
+            <md-icon>arrow_forward</md-icon>
+          </button>
         </div>
       </ng-template>
     </td-data-table>
diff --git a/securis/src/main/webapp/src/app/listing/organization.list.component.ts b/securis/src/main/webapp/src/app/listing/organization.list.component.ts
index 3d8e74d..9aebd5c 100644
--- a/securis/src/main/webapp/src/app/listing/organization.list.component.ts
+++ b/securis/src/main/webapp/src/app/listing/organization.list.component.ts
@@ -64,12 +64,11 @@
       );
   }
 
-
-  packAction(action: any) {
-    console.log(action.command);
+  showRelatedPacks(org : any) : void {
+    this.router.navigate(['packs/'], {queryParams: {organizationId: org.id, name: org.name}});
   }
 
-  isActionAvailable(pack : any) : boolean {
+  isActionAvailable(org : any) : boolean {
     return true;
   }
 
diff --git a/securis/src/main/webapp/src/app/listing/pack.list.component.html b/securis/src/main/webapp/src/app/listing/pack.list.component.html
index f826ab5..2ce0bed 100644
--- a/securis/src/main/webapp/src/app/listing/pack.list.component.html
+++ b/securis/src/main/webapp/src/app/listing/pack.list.component.html
@@ -1,5 +1,8 @@
 <td-layout-card-over cardWidth="90">
   <md-toolbar role="toolbar" class="mat-secondary">
+    <button *ngIf="!!prevUrl" md-icon-button (click)="goBack()" color="accent">
+        <md-icon>arrow_back</md-icon>
+    </button>
     <span class="push-left-sm">
         <span class="md-title" i18n>Packs</span>
     </span>
@@ -11,6 +14,15 @@
     <button md-mini-fab color="accent" (click)="create()" [mdTooltip]="$L.get('Create a new pack')">
         <md-icon>add</md-icon>
       </button>
+    <md-toolbar-row *ngIf="!!filter_description">
+      <md-chip-list>
+        <md-chip selected color="accent">{{filter_description}} </md-chip>
+      </md-chip-list>
+    <button md-icon-button (click)="reload()" >
+        <md-icon>cancel</md-icon>
+    </button>
+      <span flex></span>
+    </md-toolbar-row>
   </md-toolbar>
  	<div flex="84" layout-align="center end" layout="column">
     <td-data-table 
diff --git a/securis/src/main/webapp/src/app/listing/pack.list.component.ts b/securis/src/main/webapp/src/app/listing/pack.list.component.ts
index 9fcef79..0d92901 100644
--- a/securis/src/main/webapp/src/app/listing/pack.list.component.ts
+++ b/securis/src/main/webapp/src/app/listing/pack.list.component.ts
@@ -1,4 +1,5 @@
 import { Router, ActivatedRoute } from '@angular/router';
+import { Location } from '@angular/common';
 import { ToastsManager } from 'ng2-toastr/ng2-toastr';
 import { MdDialog, MdDialogConfig } from '@angular/material';
 import {
@@ -11,12 +12,12 @@
 import { IPageChangeEvent } from '@covalent/core';
 import { Component, ViewChild, AfterViewInit } from '@angular/core';
 import { TdMediaService } from '@covalent/core';
-import { PacksService, PACK_ACTIONS } from '../resources/packs';
+import { PacksService, PACK_ACTIONS, PacksFilter } from '../resources/packs';
 import { PackFormComponent } from '../forms/pack.form.component';
 import { LocaleService } from '../common/i18n';
 import { ListingBase } from './base';
 
-
+/**
 var pack_example = { 
   id: 7,
   code: 'DX250000',
@@ -45,7 +46,7 @@
   preactivation_valid_period: 70,
   renew_valid_period: 0,
 }
-
+*/
 @Component({
   selector: 'pack-list',
   templateUrl: 'src/app/listing/pack.list.component.html'
@@ -63,6 +64,8 @@
   ];
 
   pack_menu_options = PACK_ACTIONS;
+  filter_description : string;
+  prevUrl : string = null;
 
   constructor(_dataTableService: TdDataTableService,
     private media: TdMediaService,
@@ -76,14 +79,42 @@
       super(_dataTableService);
   }
 
-  reload() : void  {
-    this.packs.get().subscribe(
+  reload(filter?: PacksFilter) : void  {
+    this.prepareFilteredBehavior(filter);
+    this.packs.get(filter).subscribe(
       (list : any[]) => {
         this.data = list;
         this.refresh();
       },
       (err: any) => console.error(err)
     );
+  }
+
+  goBack() : void {
+    if (this.prevUrl) {
+      this.router.navigate([this.prevUrl]);
+    }
+  }
+
+  prepareFilteredBehavior(filter: PacksFilter) {
+    if (!filter) {
+      this.filter_description = '';
+      this.prevUrl = null;
+    } else {
+      if (!!filter.applicationId) {
+        this.filter_description = `Application: ${filter.name}`;
+        this.prevUrl = 'applications';
+      } else if (!!filter.organizationId) {
+        this.filter_description = `Organization: ${filter.name}`;
+        this.prevUrl = 'organizations';
+      } else if (!!filter.licenseTypeId) {
+        this.filter_description = `License type: ${filter.name}`;
+        this.prevUrl = 'licensetypes';
+      } else {
+        this.filter_description = '';
+        this.prevUrl = null;
+      }
+    }
   }
 
   packAction(action: string, pack: any) {
@@ -113,10 +144,13 @@
     this.sortOrder = sortEvent.order;
     this.refresh();
   }
-
+ 
   ngAfterViewInit(): void {
-    this.reload();
     this.media.broadcast();
+    this.route.queryParams.subscribe(params => {
+        let filter = params as PacksFilter;
+        this.reload(filter);
+      });
   }
 }
-
+ 
diff --git a/securis/src/main/webapp/src/app/resources/base.ts b/securis/src/main/webapp/src/app/resources/base.ts
index 352b641..2833eb0 100644
--- a/securis/src/main/webapp/src/app/resources/base.ts
+++ b/securis/src/main/webapp/src/app/resources/base.ts
@@ -3,8 +3,8 @@
 import { Observable } from 'rxjs/Observable';
 import { Http, RequestOptionsArgs, URLSearchParams } from '@angular/http';
 
-class MySearchParams extends URLSearchParams {
-  constructor(obj : any) {
+export class MySearchParams extends URLSearchParams {
+  constructor(obj : any = {}) {
     var searchQuery = Object.keys(obj).map(key => `${key}=${encodeURIComponent(obj[key])}`).join('&');
     super(searchQuery);
   }
diff --git a/securis/src/main/webapp/src/app/resources/packs.ts b/securis/src/main/webapp/src/app/resources/packs.ts
index 17ce29b..2bed001 100644
--- a/securis/src/main/webapp/src/app/resources/packs.ts
+++ b/securis/src/main/webapp/src/app/resources/packs.ts
@@ -1,7 +1,7 @@
 import { Observable } from 'rxjs/Rx';
 import { Injectable } from '@angular/core';
 import { Http, RequestOptions } from '@angular/http';
-import { SeCurisResourceServices } from './base';
+import { SeCurisResourceServices, MySearchParams } from './base';
 import { LocaleService } from '../common/i18n';
 
 var pack_example = { 
@@ -31,6 +31,13 @@
   organization_name: 'CurisTec',
   preactivation_valid_period: 70,
   renew_valid_period: 0,
+}
+
+export type PacksFilter = {
+    licenseTypeId?: number;
+    organizationId?: number;
+    applicationId?: number;
+    name?: string;
 }
 
 export const PACK_STATUS = {
@@ -79,6 +86,13 @@
     super($L, http, 'pack');
   }
 
+  public get(filter?: PacksFilter) {
+      let searchParams = new MySearchParams(filter);
+      let url = `${this.resource}/?${searchParams}`;
+      console.log(`url: ${url}`);
+      return this.http.get(url).map(response => response.json()).catch(err => super.processErrorResponse(err));
+	}
+
   public activate(id: number) {
 			return super.action(id, "activate");
 	}

--
Gitblit v1.3.2