001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.control.user.party.server.command; 018 019import com.echothree.control.user.party.common.edit.PartyAliasEdit; 020import com.echothree.control.user.party.common.edit.PartyEditFactory; 021import com.echothree.control.user.party.common.result.EditPartyAliasResult; 022import com.echothree.control.user.party.common.result.PartyResultFactory; 023import com.echothree.control.user.party.common.spec.PartyAliasSpec; 024import com.echothree.control.user.party.server.command.util.PartyAliasUtil; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.data.party.server.entity.PartyAlias; 029import com.echothree.model.data.party.server.entity.PartyAliasType; 030import com.echothree.util.common.command.EditMode; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.server.control.BaseAbstractEditCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import java.util.List; 039import javax.enterprise.context.Dependent; 040import javax.inject.Inject; 041 042@Dependent 043public class EditPartyAliasCommand 044 extends BaseAbstractEditCommand<PartyAliasSpec, PartyAliasEdit, EditPartyAliasResult, PartyAlias, PartyAlias> { 045 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 SPEC_FIELD_DEFINITIONS = List.of( 051 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null), 052 new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, true, null, null) 053 ); 054 055 EDIT_FIELD_DEFINITIONS = List.of( 056 new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null) 057 ); 058 } 059 060 @Inject 061 PartyControl partyControl; 062 063 064 /** Creates a new instance of EditPartyAliasCommand */ 065 public EditPartyAliasCommand() { 066 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 067 } 068 069 @Override 070 protected CommandSecurityDefinition getCommandSecurityDefinition() { 071 return new CommandSecurityDefinition(List.of( 072 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 073 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 074 new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameByPartyName(spec.getPartyName()), SecurityRoles.Edit.name()) 075 )) 076 )); 077 } 078 079 @Override 080 public EditPartyAliasResult getResult() { 081 return PartyResultFactory.getEditPartyAliasResult(); 082 } 083 084 @Override 085 public PartyAliasEdit getEdit() { 086 return PartyEditFactory.getPartyAliasEdit(); 087 } 088 089 PartyAliasType partyAliasType; 090 091 @Override 092 public PartyAlias getEntity(EditPartyAliasResult result) { 093 PartyAlias partyAlias = null; 094 var partyName = spec.getPartyName(); 095 var party = partyControl.getPartyByName(partyName); 096 097 if(party != null) { 098 var partyType = party.getLastDetail().getPartyType(); 099 var partyAliasTypeName = spec.getPartyAliasTypeName(); 100 101 partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName); 102 103 if(partyAliasType != null) { 104 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 105 partyAlias = partyControl.getPartyAlias(party, partyAliasType); 106 } else { // EditMode.UPDATE 107 partyAlias = partyControl.getPartyAliasForUpdate(party, partyAliasType); 108 } 109 110 if(partyAlias != null) { 111 result.setPartyAlias(partyControl.getPartyAliasTransfer(getUserVisit(), partyAlias)); 112 } else { 113 addExecutionError(ExecutionErrors.UnknownPartyAlias.name(), partyName, partyAliasTypeName); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), partyType.getPartyTypeName(), partyAliasTypeName); 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 120 } 121 122 return partyAlias; 123 } 124 125 @Override 126 public PartyAlias getLockEntity(PartyAlias partyAlias) { 127 return partyAlias; 128 } 129 130 @Override 131 public void fillInResult(EditPartyAliasResult result, PartyAlias partyAlias) { 132 result.setPartyAlias(partyControl.getPartyAliasTransfer(getUserVisit(), partyAlias)); 133 } 134 135 @Override 136 public void doLock(PartyAliasEdit edit, PartyAlias partyAlias) { 137 edit.setAlias(partyAlias.getAlias()); 138 } 139 140 @Override 141 public void canUpdate(PartyAlias partyAlias) { 142 var alias = edit.getAlias(); 143 var duplicatePartyAlias = partyControl.getPartyAliasByAlias(partyAliasType, alias); 144 145 if(duplicatePartyAlias != null && !partyAlias.equals(duplicatePartyAlias)) { 146 var partyAliasTypeDetail = partyAlias.getPartyAliasType().getLastDetail(); 147 148 addExecutionError(ExecutionErrors.DuplicatePartyAlias.name(), partyAliasTypeDetail.getPartyType().getPartyTypeName(), 149 partyAliasTypeDetail.getPartyAliasTypeName(), alias); 150 } 151 } 152 153 @Override 154 public void doUpdate(PartyAlias partyAlias) { 155 var partyAliasValue = partyControl.getPartyAliasValue(partyAlias); 156 157 partyAliasValue.setAlias(edit.getAlias()); 158 159 partyControl.updatePartyAliasFromValue(partyAliasValue, getPartyPK()); 160 } 161 162}