github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/Decentralized-Energy-Composer-master/angular-app/src/app/UtilityCompany/UtilityCompany.component.ts (about)

     1  import { Component, OnInit, Input } from '@angular/core';
     2  import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
     3  import { UtilityCompanyService } from './UtilityCompany.service';
     4  import 'rxjs/add/operator/toPromise';
     5  
     6  //provide associated components
     7  @Component({
     8  	selector: 'app-UtilityCompany',
     9  	templateUrl: './UtilityCompany.component.html',
    10  	styleUrls: ['./UtilityCompany.component.css'],
    11    	providers: [UtilityCompanyService]
    12  })
    13  
    14  //UtilityCompanyComponent class
    15  export class UtilityCompanyComponent {
    16  
    17    //define variables
    18    myForm: FormGroup;
    19  
    20    private allUtilityCompanys;
    21    private utilityCompany;
    22    private currentId;
    23    private errorMessage;
    24  
    25    private coins;
    26    private energy;
    27    
    28    //initialize form variables
    29    utilityID = new FormControl("", Validators.required);
    30    name = new FormControl("", Validators.required);
    31    coinsValue = new FormControl("", Validators.required);
    32    energyValue = new FormControl("", Validators.required);
    33    energyUnits = new FormControl("", Validators.required);
    34        
    35    constructor(private serviceUtilityCompany:UtilityCompanyService, fb: FormBuilder) {
    36      //intialize form
    37      this.myForm = fb.group({         
    38            utilityID:this.utilityID,
    39            name:this.name,      
    40            coinsValue:this.coinsValue,
    41            energyValue:this.energyValue,
    42            energyUnits:this.energyUnits,
    43      });
    44    };
    45  
    46    //on page initialize, load all utility companies
    47    ngOnInit(): void {
    48      this.loadAll();
    49    }
    50  
    51    //load all Utility Companies and the coins and energy assets associated to it 
    52    loadAll(): Promise<any>  {
    53      
    54      //retrieve all utilityCompanys in the utilityCompanyList array
    55      let utilityCompanyList = [];
    56  
    57      //call serviceUtilityCompany to get all utility company objects
    58      return this.serviceUtilityCompany.getAllUtilityCompanys()
    59      .toPromise()
    60      .then((result) => {
    61        this.errorMessage = null;
    62        
    63        //append utilityCompanyList with the utility company objects returned
    64        result.forEach(utilityCompany => {
    65          utilityCompanyList.push(utilityCompany);
    66        });     
    67      })
    68      .then(() => {
    69  
    70        //for each utility company, get the associated coins and energy asset
    71        for (let utilityCompany of utilityCompanyList) {
    72  
    73          //get coinsID from the utilityCompany.coins string
    74          var splitted_coinsID = utilityCompany.coins.split("#", 2); 
    75          var coinsID = String(splitted_coinsID[1]);
    76  
    77          //call serviceUtilityCompany to get coins asset
    78          this.serviceUtilityCompany.getCoins(coinsID)
    79          .toPromise()
    80          .then((result) => {
    81            this.errorMessage = null;
    82  
    83            //update utilityCompany
    84            if(result.value){
    85              utilityCompany.coinsValue = result.value;
    86            }
    87          });
    88  
    89          //get energyID from the utilityCompany.energy string
    90          var splitted_energyID = utilityCompany.energy.split("#", 2); 
    91          var energyID = String(splitted_energyID[1]);
    92          
    93          //call serviceUtilityCompany to get energy asset
    94          this.serviceUtilityCompany.getEnergy(energyID)
    95          .toPromise()
    96          .then((result) => {
    97            this.errorMessage = null;
    98  
    99            //update utilityCompany
   100            if(result.value){
   101              utilityCompany.energyValue = result.value;
   102            }
   103            if(result.units){
   104              utilityCompany.energyUnits = result.units;
   105            }
   106          });
   107          
   108        }
   109  
   110        //assign utilityCompanyList to allUtilityCompanys
   111        this.allUtilityCompanys = utilityCompanyList;
   112      });
   113  
   114    }
   115  
   116    //add Utility Company participant
   117    addUtilityCompany(form: any): Promise<any> {
   118  
   119      //create assets for utility company and the utility company on the blockchain network
   120      return this.createAssetsUtility()
   121        .then(() => {           
   122          this.errorMessage = null;
   123          this.myForm.setValue({
   124              "utilityID":null,
   125              "name":null,
   126              "coinsValue":null,
   127              "energyValue":null,          
   128              "energyUnits":null
   129          });
   130        })
   131      .catch((error) => {
   132          if(error == 'Server error'){
   133              this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   134          }
   135          else if (error == '500 - Internal Server Error') {
   136            this.errorMessage = "Input error";
   137          }
   138          else{
   139              this.errorMessage = error;
   140          }
   141      });
   142    }
   143  
   144    //create coins and energy assets associated with the Resident, followed by the Resident
   145    createAssetsUtility(): Promise<any> {
   146  
   147      //create coins asset json
   148      this.coins = {
   149        $class: "org.decentralized.energy.network.Coins",
   150            "coinsID":"CO_" + this.utilityID.value,
   151            "value":this.coinsValue.value,
   152            "ownerID":this.utilityID.value,
   153            "ownerEntity":'UtilityCompany'
   154      };
   155      
   156      //create energy asset json
   157      this.energy = {
   158        $class: "org.decentralized.energy.network.Energy",
   159            "energyID":"EN_" + this.utilityID.value,
   160            "units":this.energyUnits.value,
   161            "value":this.energyValue.value,
   162            "ownerID":this.utilityID.value,
   163            "ownerEntity":'UtilityCompany'        
   164      };
   165      
   166      //create utility company participant json
   167      this.utilityCompany = {
   168        $class: "org.decentralized.energy.network.UtilityCompany",
   169            "utilityID":this.utilityID.value,
   170            "name":this.name.value,
   171            "coins":"CO_" + this.utilityID.value,
   172            "energy":"EN_" + this.utilityID.value,
   173      };    
   174  
   175      //call serviceUtilityCompany to add coins asset, pass created coins asset json as parameter
   176      return this.serviceUtilityCompany.addCoins(this.coins)
   177      .toPromise()
   178  		.then(() => {
   179        
   180        //call serviceUtilityCompany to add energy asset, pass created energy asset json as parameter
   181  			this.serviceUtilityCompany.addEnergy(this.energy)      
   182        .toPromise()
   183        .then(() => {
   184          
   185          //call serviceUtilityCompany to add utility participant, pass created utility participant json as parameter
   186          this.serviceUtilityCompany.addUtilityCompany(this.utilityCompany)
   187          .toPromise()
   188          .then(() => {
   189            //reload page to display the created utility company
   190            location.reload();                  
   191          });
   192  		  });   
   193  		});
   194    }
   195  
   196    //allow update name of Utility Company
   197    updateUtilityCompany(form: any): Promise<any> {
   198      
   199      //create json of utility company participant to update name
   200      this.utilityCompany = {
   201        $class: "org.decentralized.energy.network.UtilityCompany",          
   202                "name":this.name.value,                    
   203                "coins": "resource:org.decentralized.energy.network.Coins#CO_" + form.get("utilityID").value,
   204                "energy": "resource:org.decentralized.energy.network.Energy#EN_" + form.get("utilityID").value
   205      };
   206  
   207      //call serviceUtilityCompany to update utility company, pass utilityID of which utility company to update as parameter
   208      return this.serviceUtilityCompany.updateUtilityCompany(form.get("utilityID").value,this.utilityCompany)
   209  		.toPromise()
   210  		.then(() => {
   211  			this.errorMessage = null;
   212  		})
   213  		.catch((error) => {
   214              if(error == 'Server error'){
   215  				this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   216  			}
   217              else if(error == '404 - Not Found'){
   218  				this.errorMessage = "404 - Could not find API route. Please check your available APIs."
   219  			}
   220  			else{
   221  				this.errorMessage = error;
   222  			}
   223      });
   224    }
   225  
   226    //delete Utility Company and the coins and energy assets associated to it
   227    deleteUtilityCompany(): Promise<any> {
   228  
   229      //call serviceUtilityCompany to delete utilty company, pass utilityID as parameter
   230      return this.serviceUtilityCompany.deleteUtilityCompany(this.currentId)
   231  		.toPromise()
   232  		.then(() => {
   233  			this.errorMessage = null;
   234  
   235        //call serviceUtilityCompany to delete coins asset, pass coinsID as parameter
   236        this.serviceUtilityCompany.deleteCoins("CO_"+this.currentId)
   237        .toPromise()
   238        .then(() => {
   239  
   240            //call serviceUtilityCompany to delete energy asset, pass energyID as parameter
   241            this.serviceUtilityCompany.deleteEnergy("EN_"+this.currentId)
   242            .toPromise()
   243            .then(() => {
   244                console.log("Deleted")              
   245            });
   246        });            
   247  		})
   248  		.catch((error) => {
   249              if(error == 'Server error'){
   250  				this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   251  			}
   252  			else if(error == '404 - Not Found'){
   253  				this.errorMessage = "404 - Could not find API route. Please check your available APIs."
   254  			}
   255  			else{
   256  				this.errorMessage = error;
   257  			}
   258      });
   259    }
   260  
   261    //set id
   262    setId(id: any): void{
   263      this.currentId = id;
   264    }
   265  
   266    //get form based on utilityID
   267    getForm(id: any): Promise<any>{
   268  
   269      //call serviceUtilityCompany to get utility company participant object
   270      return this.serviceUtilityCompany.getUtilityCompany(id)
   271      .toPromise()
   272      .then((result) => {
   273  			this.errorMessage = null;
   274        let formObject = {        
   275              "utilityID":null,          
   276              "name":null,          
   277              "coinsValue":null,          
   278              "energyValue":null,          
   279              "energyUnits":null                               
   280        };
   281  
   282        //update formObject
   283        if(result.utilityID){
   284          formObject.utilityID = result.utilityID;
   285        }else{
   286          formObject.utilityID = null;
   287        }
   288      
   289        if(result.name){
   290          formObject.name = result.name;
   291        }else{
   292          formObject.name = null;
   293        }      
   294  
   295        this.myForm.setValue(formObject);
   296  
   297      })
   298      .catch((error) => {
   299          if(error == 'Server error'){
   300              this.errorMessage = "Could not connect to REST server. Please check your configuration details";
   301          }
   302          else if(error == '404 - Not Found'){
   303  				this.errorMessage = "404 - Could not find API route. Please check your available APIs."
   304          }
   305          else{
   306              this.errorMessage = error;
   307          }
   308      });
   309  
   310    }
   311  
   312    //reset form
   313    resetForm(): void{
   314      this.myForm.setValue({           
   315            "utilityID":null, 
   316            "name":null,                 
   317  
   318            "coinsValue":null,
   319            "energyValue":null,
   320            "energyUnits":null,
   321        });
   322    }
   323  
   324  }
   325  
   326  
   327  
   328  
   329