Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit, Input } from '@angular/core';
- import { Http, Response, Headers, RequestOptions } from "@angular/http";
- import { Growl, InputTextModule, ButtonModule, Message } from 'primeng/primeng';
- import { DocumentRequestModel } from './document.request.model';
- import { DocumentResponseModel } from './document.response.model';
- import { CafDocRequestModel } from './cafdoc.request.model';
- import { CafDocResponseModel } from './cafdoc.response.model';
- import { DocumentDisplayModel } from './document.display.model';
- import 'rxjs/add/operator/map';
- import { Observable } from 'rxjs';
- import { ConceptService } from '../concept.service';
- import { environment } from '../../../environments/environment';
- @Component({
- selector: 'app-uploaddocument',
- templateUrl: './uploaddocument.component.html',
- styleUrls: ['./uploaddocument.component.scss'],
- providers: [ConceptService]
- })
- export class UploaddocumentComponent implements OnInit {
- // naming convention C_Concept_id_Analtyic_client_id_filename.xxx (extn)
- public documentDisplayModelList: DocumentDisplayModel[] = new Array();
- public downloadUrlPrefix: string;
- public updateIndex: number = -1;
- public isDeleteButtonExist: boolean;
- ifFileUploaded: boolean;
- isFileSelected: boolean;
- isFileUploadButtonEnabled: boolean;
- isUpdateOperation: boolean;
- objectType: string;
- public finalStatusCode: number;
- isDuplicateDocuments = [];
- docRequest = [];
- @Input() conceptId: number;
- @Input() entityId: number;
- @Input() entityType: string;
- @Input() isRequestFromAdd: boolean;
- @Input() isRequestFromView: boolean;
- // if it is not new,, get existing documents and show to the user
- @Input() isNew: string;
- tempFileData;
- tempFileName;
- alfrescoUrl: string = environment.alfrescoUrlForUpload;
- form: FormData = new FormData();
- ngOnInit() {
- this.downloadUrlPrefix = this.getDownloadUrl();
- this.ifFileUploaded = false;
- this.isFileSelected = false;
- this.isUpdateOperation = false;
- let fileListData: CafDocResponseModel[];
- if (this.isRequestFromAdd == false) {
- this.isDeleteButtonExist = false;
- } else {
- this.isDeleteButtonExist = true;
- }
- if (this.isRequestFromAdd == false) {
- //call back end and get documents show the data in rows
- this.getDocumentDisplayModels(this.entityId, this.entityType);
- }
- if ((typeof (this.entityId) !== "undefined") && (this.entityId !== null)) {
- this.isFileUploadButtonEnabled = true;
- } else {
- this.isFileUploadButtonEnabled = false;
- }
- this.isFileUploadButtonEnabled = true;
- if ((typeof (this.entityType) !== "undefined") && (this.entityType !== null)) {
- this.objectType = this.entityType;
- }
- }
- constructor(private http: Http, private conceptService: ConceptService) {
- }
- getDocumentDisplayModels(entityId: number, entityType: string) {
- this.documentDisplayModelList = new Array();
- this.conceptService.getAnalyticDocuments(entityId, entityType).subscribe(result => {
- let cafDocResponseData: CafDocResponseModel[] = result;
- let tempDisplayModel: DocumentDisplayModel;
- if (cafDocResponseData.length > 0) {
- for (let tempResponse of cafDocResponseData) {
- tempDisplayModel = new DocumentDisplayModel();
- tempDisplayModel.displayFileName = tempResponse.name;
- tempDisplayModel.documentSourceId = tempResponse.documentSourceId;
- tempDisplayModel.downloadURL = this.downloadUrlPrefix + tempResponse.documentSourceId;
- this.documentDisplayModelList.push(tempDisplayModel);
- }
- }
- });
- }
- public fileChangeEvent(files) {
- if (files.length > 0) {
- for (var i = 0; i < files.length; i++) {
- this.docRequest[i] = new DocumentDisplayModel();
- this.docRequest[i].filedata = files[i];
- this.docRequest[i].filename = files[i].name;
- // C_Concept_id_Analtyic_client_id_filename.xxx (extn)
- if (this.objectType === "CONCEPT") {
- this.docRequest[i].filename = "C_" + this.entityId + "__" + this.docRequest[i].filename;
- } else {
- this.docRequest[i].filename = "C_" + this.conceptId + "_" + this.entityId + "_" + this.docRequest[i].filename;
- }
- }
- this.isFileSelected = true;
- }
- }
- getDownloadUrl(): string {
- let downloadUrl: string = environment.alfrescoDownloadUrlPrefix;
- let downloadUrlWithSecurity: string = "http://" + environment.cafUserId + ":" +
- environment.cafPassowrd + "@" + environment.alfrescoHost + ":" + environment.alfrescoPort + downloadUrl;
- return downloadUrlWithSecurity;
- }
- uploadFileByRest() {
- //check for duplicate file entry
- for (var i = 0; i < this.documentDisplayModelList.length; i++) {
- this.isDuplicateDocuments.push(this.documentDisplayModelList[i].displayFileName);
- }
- // as no duplicates,, upload file
- this.doUploadByRest();
- }
- download (file) {
- var jsonRequest = JSON.stringify({username:environment.cafUserId, password:environment.cafPassowrd});
- var headers = new Headers();
- headers.append('Content-Type', 'application/json');
- headers.append('Authorization', 'Basic ' + btoa(environment.alfrescoCredentials));
- this.http.post('http://' + environment.alfrescoHost + ":" + environment.alfrescoPort +
- '/alfresco/service/api/login', jsonRequest, {headers: headers})
- .map(res => {return res.json()})
- .subscribe(
- data => {
- this.http.get('http://' + environment.alfrescoHost + ":" + environment.alfrescoPort + environment.alfrescoDownloadUrlPrefix + file.documentSourceId
- + '?alf_ticket=' + data.data.ticket, {headers: headers})
- .toPromise()
- .then(res => {
- window.open(res.url);
- });
- });
- }
- delete (file, fileIndex) {
- var headers = new Headers();
- headers.append('Content-Type', 'application/json');
- headers.append('Authorization', 'Basic ' + btoa(environment.alfrescoCredentials));
- this.http.delete(environment.alfrescoUrl + 'nodes/' + file.documentSourceId, {headers: headers})
- .map(res => {
- if (res.status == 204) {
- this.http.delete(environment.baseUrl + 'analyticDocument/delete/' + file.documentSourceId)
- .map(response => {
- this.documentDisplayModelList.splice(fileIndex, 1);
- })
- .subscribe();
- }
- })
- .subscribe();
- }
- doUploadByRest() {
- var tempHttp: Http = this.http;
- for (var i = 0; i < this.docRequest.length; i++) {
- this.tempFileData = this.docRequest[i].filedata;
- this.tempFileName = this.docRequest[i].filename;
- this.form.append("name", this.tempFileName);
- this.form.append('filedata', this.tempFileData);
- this.form.append('overwrite', 'true');
- this.form.append('nodeType', 'cm:content');
- var headersData = new Headers();
- headersData.append('Authorization', 'Basic ' + btoa(environment.alfrescoCredentials));
- tempHttp.post(this.alfrescoUrl, this.form, { headers: headersData }).map((res: Response) => {
- // write a logic ,, so that it will make calls to CAR-API
- if (res.status == 201) {
- let downloadUrl2: string
- var respJson = res.json();
- var entry = respJson.entry;
- this.ifFileUploaded = true;
- let cafDocReq: CafDocRequestModel = new CafDocRequestModel();
- if (this.entityType === "CONCEPT") {
- cafDocReq.conceptId = this.entityId;
- cafDocReq.analyticId = null;
- } else {
- cafDocReq.conceptId = null;
- cafDocReq.analyticId = this.entityId;
- }
- cafDocReq.createdBy = entry.createdByUser.id;
- cafDocReq.createdDate = entry.createdAt;
- cafDocReq.modifiedBy = entry.modifiedByUser.id;
- cafDocReq.documentSourceId = entry.id;
- cafDocReq.name = entry.name;
- cafDocReq.path = entry.id;
- cafDocReq.previousDocumentSourceId = null;
- if (!this.isUpdateOperation) {
- if (this.isDuplicateDocuments.indexOf(entry.name) === -1) {
- //call cAF server
- this.conceptService.addAnalyticDocument(cafDocReq).subscribe(result => {
- this.finalStatusCode = result as number;
- if (this.finalStatusCode == 200) {
- let tempDocDisplayModel: DocumentDisplayModel = new DocumentDisplayModel();
- console.log("ADD file uploaded successfully");
- tempDocDisplayModel.displayFileName = entry.name;
- tempDocDisplayModel.downloadURL = this.downloadUrlPrefix + entry.id;
- tempDocDisplayModel.documentSourceId = entry.id;
- //push the record
- this.documentDisplayModelList.push(tempDocDisplayModel);
- } else {
- alert("ADD Error on uploading file");
- }
- });
- //update is done.. so change the toggle
- this.isUpdateOperation = false;
- }
- } else {
- // pass the previous document id
- cafDocReq.previousDocumentSourceId = this.documentDisplayModelList[this.updateIndex].documentSourceId;
- this.conceptService.updateAnalyticDocument(this.entityType, cafDocReq).subscribe(result => {
- this.finalStatusCode = result as number;
- if (this.finalStatusCode == 200) {
- console.log("UPDATE file updated successfully");
- let tempDocDisplayModel: DocumentDisplayModel = new DocumentDisplayModel();
- tempDocDisplayModel.displayFileName = entry.name;
- tempDocDisplayModel.downloadURL = this.downloadUrlPrefix + entry.id;
- tempDocDisplayModel.documentSourceId = entry.id;
- this.documentDisplayModelList[this.updateIndex] = tempDocDisplayModel;
- this.updateIndex = -1;
- //as file is updated,, change the flag value
- this.isUpdateOperation = false;
- } else {
- alert(" UPDATE Error on updating file");
- }
- });
- }
- //call CAF server ends
- }
- }).subscribe();
- }
- this.getDocumentDisplayModels(this.entityId, this.entityType);
- }//uploadFileByRest ends here
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement