Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. import { Component, OnInit, Input } from '@angular/core';
  2. import { Http, Response, Headers, RequestOptions } from "@angular/http";
  3. import { Growl, InputTextModule, ButtonModule, Message } from 'primeng/primeng';
  4. import { DocumentRequestModel } from './document.request.model';
  5. import { DocumentResponseModel } from './document.response.model';
  6. import { CafDocRequestModel } from './cafdoc.request.model';
  7. import { CafDocResponseModel } from './cafdoc.response.model';
  8. import { DocumentDisplayModel } from './document.display.model';
  9. import 'rxjs/add/operator/map';
  10. import { Observable } from 'rxjs';
  11. import { ConceptService } from '../concept.service';
  12. import { environment } from '../../../environments/environment';
  13.  
  14. @Component({
  15. selector: 'app-uploaddocument',
  16. templateUrl: './uploaddocument.component.html',
  17. styleUrls: ['./uploaddocument.component.scss'],
  18. providers: [ConceptService]
  19.  
  20. })
  21. export class UploaddocumentComponent implements OnInit {
  22.  
  23. // naming convention C_Concept_id_Analtyic_client_id_filename.xxx (extn)
  24. public documentDisplayModelList: DocumentDisplayModel[] = new Array();
  25. public downloadUrlPrefix: string;
  26. public updateIndex: number = -1;
  27. public isDeleteButtonExist: boolean;
  28. ifFileUploaded: boolean;
  29. isFileSelected: boolean;
  30. isFileUploadButtonEnabled: boolean;
  31. isUpdateOperation: boolean;
  32. objectType: string;
  33. public finalStatusCode: number;
  34. isDuplicateDocuments = [];
  35. docRequest = [];
  36. @Input() conceptId: number;
  37. @Input() entityId: number;
  38. @Input() entityType: string;
  39. @Input() isRequestFromAdd: boolean;
  40. @Input() isRequestFromView: boolean;
  41. // if it is not new,, get existing documents and show to the user
  42. @Input() isNew: string;
  43. tempFileData;
  44. tempFileName;
  45. alfrescoUrl: string = environment.alfrescoUrlForUpload;
  46. form: FormData = new FormData();
  47.  
  48. ngOnInit() {
  49. this.downloadUrlPrefix = this.getDownloadUrl();
  50. this.ifFileUploaded = false;
  51. this.isFileSelected = false;
  52. this.isUpdateOperation = false;
  53. let fileListData: CafDocResponseModel[];
  54.  
  55. if (this.isRequestFromAdd == false) {
  56. this.isDeleteButtonExist = false;
  57. } else {
  58. this.isDeleteButtonExist = true;
  59. }
  60.  
  61. if (this.isRequestFromAdd == false) {
  62. //call back end and get documents show the data in rows
  63. this.getDocumentDisplayModels(this.entityId, this.entityType);
  64. }
  65.  
  66. if ((typeof (this.entityId) !== "undefined") && (this.entityId !== null)) {
  67. this.isFileUploadButtonEnabled = true;
  68. } else {
  69. this.isFileUploadButtonEnabled = false;
  70. }
  71.  
  72. this.isFileUploadButtonEnabled = true;
  73. if ((typeof (this.entityType) !== "undefined") && (this.entityType !== null)) {
  74. this.objectType = this.entityType;
  75.  
  76. }
  77. }
  78.  
  79. constructor(private http: Http, private conceptService: ConceptService) {
  80. }
  81.  
  82. getDocumentDisplayModels(entityId: number, entityType: string) {
  83. this.documentDisplayModelList = new Array();
  84. this.conceptService.getAnalyticDocuments(entityId, entityType).subscribe(result => {
  85. let cafDocResponseData: CafDocResponseModel[] = result;
  86. let tempDisplayModel: DocumentDisplayModel;
  87. if (cafDocResponseData.length > 0) {
  88. for (let tempResponse of cafDocResponseData) {
  89. tempDisplayModel = new DocumentDisplayModel();
  90. tempDisplayModel.displayFileName = tempResponse.name;
  91. tempDisplayModel.documentSourceId = tempResponse.documentSourceId;
  92. tempDisplayModel.downloadURL = this.downloadUrlPrefix + tempResponse.documentSourceId;
  93. this.documentDisplayModelList.push(tempDisplayModel);
  94. }
  95. }
  96. });
  97. }
  98.  
  99. public fileChangeEvent(files) {
  100. if (files.length > 0) {
  101. for (var i = 0; i < files.length; i++) {
  102. this.docRequest[i] = new DocumentDisplayModel();
  103. this.docRequest[i].filedata = files[i];
  104. this.docRequest[i].filename = files[i].name;
  105. // C_Concept_id_Analtyic_client_id_filename.xxx (extn)
  106. if (this.objectType === "CONCEPT") {
  107. this.docRequest[i].filename = "C_" + this.entityId + "__" + this.docRequest[i].filename;
  108. } else {
  109. this.docRequest[i].filename = "C_" + this.conceptId + "_" + this.entityId + "_" + this.docRequest[i].filename;
  110. }
  111. }
  112. this.isFileSelected = true;
  113. }
  114. }
  115.  
  116. getDownloadUrl(): string {
  117. let downloadUrl: string = environment.alfrescoDownloadUrlPrefix;
  118. let downloadUrlWithSecurity: string = "http://" + environment.cafUserId + ":" +
  119. environment.cafPassowrd + "@" + environment.alfrescoHost + ":" + environment.alfrescoPort + downloadUrl;
  120. return downloadUrlWithSecurity;
  121. }
  122.  
  123. uploadFileByRest() {
  124. //check for duplicate file entry
  125. for (var i = 0; i < this.documentDisplayModelList.length; i++) {
  126. this.isDuplicateDocuments.push(this.documentDisplayModelList[i].displayFileName);
  127. }
  128. // as no duplicates,, upload file
  129. this.doUploadByRest();
  130. }
  131.  
  132. download (file) {
  133. var jsonRequest = JSON.stringify({username:environment.cafUserId, password:environment.cafPassowrd});
  134. var headers = new Headers();
  135. headers.append('Content-Type', 'application/json');
  136. headers.append('Authorization', 'Basic ' + btoa(environment.alfrescoCredentials));
  137. this.http.post('http://' + environment.alfrescoHost + ":" + environment.alfrescoPort +
  138. '/alfresco/service/api/login', jsonRequest, {headers: headers})
  139. .map(res => {return res.json()})
  140. .subscribe(
  141. data => {
  142. this.http.get('http://' + environment.alfrescoHost + ":" + environment.alfrescoPort + environment.alfrescoDownloadUrlPrefix + file.documentSourceId
  143. + '?alf_ticket=' + data.data.ticket, {headers: headers})
  144. .toPromise()
  145. .then(res => {
  146. window.open(res.url);
  147. });
  148. });
  149. }
  150.  
  151. delete (file, fileIndex) {
  152. var headers = new Headers();
  153. headers.append('Content-Type', 'application/json');
  154. headers.append('Authorization', 'Basic ' + btoa(environment.alfrescoCredentials));
  155. this.http.delete(environment.alfrescoUrl + 'nodes/' + file.documentSourceId, {headers: headers})
  156. .map(res => {
  157. if (res.status == 204) {
  158. this.http.delete(environment.baseUrl + 'analyticDocument/delete/' + file.documentSourceId)
  159. .map(response => {
  160. this.documentDisplayModelList.splice(fileIndex, 1);
  161. })
  162. .subscribe();
  163. }
  164. })
  165. .subscribe();
  166. }
  167.  
  168. doUploadByRest() {
  169. var tempHttp: Http = this.http;
  170. for (var i = 0; i < this.docRequest.length; i++) {
  171. this.tempFileData = this.docRequest[i].filedata;
  172. this.tempFileName = this.docRequest[i].filename;
  173.  
  174. this.form.append("name", this.tempFileName);
  175. this.form.append('filedata', this.tempFileData);
  176. this.form.append('overwrite', 'true');
  177. this.form.append('nodeType', 'cm:content');
  178. var headersData = new Headers();
  179. headersData.append('Authorization', 'Basic ' + btoa(environment.alfrescoCredentials));
  180. tempHttp.post(this.alfrescoUrl, this.form, { headers: headersData }).map((res: Response) => {
  181. // write a logic ,, so that it will make calls to CAR-API
  182. if (res.status == 201) {
  183. let downloadUrl2: string
  184. var respJson = res.json();
  185. var entry = respJson.entry;
  186. this.ifFileUploaded = true;
  187.  
  188. let cafDocReq: CafDocRequestModel = new CafDocRequestModel();
  189.  
  190. if (this.entityType === "CONCEPT") {
  191. cafDocReq.conceptId = this.entityId;
  192. cafDocReq.analyticId = null;
  193. } else {
  194. cafDocReq.conceptId = null;
  195. cafDocReq.analyticId = this.entityId;
  196. }
  197. cafDocReq.createdBy = entry.createdByUser.id;
  198. cafDocReq.createdDate = entry.createdAt;
  199. cafDocReq.modifiedBy = entry.modifiedByUser.id;
  200. cafDocReq.documentSourceId = entry.id;
  201. cafDocReq.name = entry.name;
  202. cafDocReq.path = entry.id;
  203. cafDocReq.previousDocumentSourceId = null;
  204. if (!this.isUpdateOperation) {
  205. if (this.isDuplicateDocuments.indexOf(entry.name) === -1) {
  206. //call cAF server
  207. this.conceptService.addAnalyticDocument(cafDocReq).subscribe(result => {
  208. this.finalStatusCode = result as number;
  209. if (this.finalStatusCode == 200) {
  210.  
  211. let tempDocDisplayModel: DocumentDisplayModel = new DocumentDisplayModel();
  212. console.log("ADD file uploaded successfully");
  213. tempDocDisplayModel.displayFileName = entry.name;
  214. tempDocDisplayModel.downloadURL = this.downloadUrlPrefix + entry.id;
  215. tempDocDisplayModel.documentSourceId = entry.id;
  216. //push the record
  217. this.documentDisplayModelList.push(tempDocDisplayModel);
  218. } else {
  219. alert("ADD Error on uploading file");
  220. }
  221. });
  222. //update is done.. so change the toggle
  223. this.isUpdateOperation = false;
  224. }
  225. } else {
  226. // pass the previous document id
  227. cafDocReq.previousDocumentSourceId = this.documentDisplayModelList[this.updateIndex].documentSourceId;
  228. this.conceptService.updateAnalyticDocument(this.entityType, cafDocReq).subscribe(result => {
  229. this.finalStatusCode = result as number;
  230. if (this.finalStatusCode == 200) {
  231. console.log("UPDATE file updated successfully");
  232.  
  233. let tempDocDisplayModel: DocumentDisplayModel = new DocumentDisplayModel();
  234. tempDocDisplayModel.displayFileName = entry.name;
  235. tempDocDisplayModel.downloadURL = this.downloadUrlPrefix + entry.id;
  236. tempDocDisplayModel.documentSourceId = entry.id;
  237. this.documentDisplayModelList[this.updateIndex] = tempDocDisplayModel;
  238. this.updateIndex = -1;
  239. //as file is updated,, change the flag value
  240. this.isUpdateOperation = false;
  241. } else {
  242. alert(" UPDATE Error on updating file");
  243. }
  244. });
  245. }
  246. //call CAF server ends
  247. }
  248. }).subscribe();
  249. }
  250. this.getDocumentDisplayModels(this.entityId, this.entityType);
  251. }//uploadFileByRest ends here
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement