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 com.echothree.util.server.persistence.Session; 039import java.util.List; 040import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditPartyAliasCommand */ 061 public EditPartyAliasCommand() { 062 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 063 } 064 065 @Override 066 protected CommandSecurityDefinition getCommandSecurityDefinition() { 067 return new CommandSecurityDefinition(List.of( 068 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 069 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 070 new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameByPartyName(spec.getPartyName()), SecurityRoles.Edit.name()) 071 )) 072 )); 073 } 074 075 @Override 076 public EditPartyAliasResult getResult() { 077 return PartyResultFactory.getEditPartyAliasResult(); 078 } 079 080 @Override 081 public PartyAliasEdit getEdit() { 082 return PartyEditFactory.getPartyAliasEdit(); 083 } 084 085 PartyAliasType partyAliasType; 086 087 @Override 088 public PartyAlias getEntity(EditPartyAliasResult result) { 089 var partyControl = Session.getModelController(PartyControl.class); 090 PartyAlias partyAlias = null; 091 var partyName = spec.getPartyName(); 092 var party = partyControl.getPartyByName(partyName); 093 094 if(party != null) { 095 var partyType = party.getLastDetail().getPartyType(); 096 var partyAliasTypeName = spec.getPartyAliasTypeName(); 097 098 partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName); 099 100 if(partyAliasType != null) { 101 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 102 partyAlias = partyControl.getPartyAlias(party, partyAliasType); 103 } else { // EditMode.UPDATE 104 partyAlias = partyControl.getPartyAliasForUpdate(party, partyAliasType); 105 } 106 107 if(partyAlias != null) { 108 result.setPartyAlias(partyControl.getPartyAliasTransfer(getUserVisit(), partyAlias)); 109 } else { 110 addExecutionError(ExecutionErrors.UnknownPartyAlias.name(), partyName, partyAliasTypeName); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), partyType.getPartyTypeName(), partyAliasTypeName); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 117 } 118 119 return partyAlias; 120 } 121 122 @Override 123 public PartyAlias getLockEntity(PartyAlias partyAlias) { 124 return partyAlias; 125 } 126 127 @Override 128 public void fillInResult(EditPartyAliasResult result, PartyAlias partyAlias) { 129 var partyControl = Session.getModelController(PartyControl.class); 130 131 result.setPartyAlias(partyControl.getPartyAliasTransfer(getUserVisit(), partyAlias)); 132 } 133 134 @Override 135 public void doLock(PartyAliasEdit edit, PartyAlias partyAlias) { 136 edit.setAlias(partyAlias.getAlias()); 137 } 138 139 @Override 140 public void canUpdate(PartyAlias partyAlias) { 141 var partyControl = Session.getModelController(PartyControl.class); 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 partyControl = Session.getModelController(PartyControl.class); 156 var partyAliasValue = partyControl.getPartyAliasValue(partyAlias); 157 158 partyAliasValue.setAlias(edit.getAlias()); 159 160 partyControl.updatePartyAliasFromValue(partyAliasValue, getPartyPK()); 161 } 162 163}