001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.form.EditPartyAliasForm; 022import com.echothree.control.user.party.common.result.EditPartyAliasResult; 023import com.echothree.control.user.party.common.result.PartyResultFactory; 024import com.echothree.control.user.party.common.spec.PartyAliasSpec; 025import com.echothree.control.user.party.server.command.util.PartyAliasUtil; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.party.server.entity.Party; 030import com.echothree.model.data.party.server.entity.PartyAlias; 031import com.echothree.model.data.party.server.entity.PartyAliasType; 032import com.echothree.model.data.party.server.entity.PartyAliasTypeDetail; 033import com.echothree.model.data.party.server.entity.PartyType; 034import com.echothree.model.data.party.server.value.PartyAliasValue; 035import com.echothree.model.data.user.common.pk.UserVisitPK; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.common.command.EditMode; 040import com.echothree.util.server.control.BaseAbstractEditCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import com.echothree.util.server.persistence.Session; 045import java.util.Arrays; 046import java.util.Collections; 047import java.util.List; 048 049public class EditPartyAliasCommand 050 extends BaseAbstractEditCommand<PartyAliasSpec, PartyAliasEdit, EditPartyAliasResult, PartyAlias, PartyAlias> { 051 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 057 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null), 058 new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, true, null, null) 059 )); 060 061 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null) 063 )); 064 } 065 066 /** Creates a new instance of EditPartyAliasCommand */ 067 public EditPartyAliasCommand(UserVisitPK userVisitPK, EditPartyAliasForm form) { 068 super(userVisitPK, form, new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 069 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 070 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 071 new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameByPartyName(form == null ? null : form.getSpec().getPartyName()), SecurityRoles.Edit.name()) 072 ))) 073 ))), SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 public EditPartyAliasResult getResult() { 078 return PartyResultFactory.getEditPartyAliasResult(); 079 } 080 081 @Override 082 public PartyAliasEdit getEdit() { 083 return PartyEditFactory.getPartyAliasEdit(); 084 } 085 086 PartyAliasType partyAliasType; 087 088 @Override 089 public PartyAlias getEntity(EditPartyAliasResult result) { 090 var partyControl = Session.getModelController(PartyControl.class); 091 PartyAlias partyAlias = null; 092 String partyName = spec.getPartyName(); 093 Party party = partyControl.getPartyByName(partyName); 094 095 if(party != null) { 096 PartyType partyType = party.getLastDetail().getPartyType(); 097 String partyAliasTypeName = spec.getPartyAliasTypeName(); 098 099 partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName); 100 101 if(partyAliasType != null) { 102 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 103 partyAlias = partyControl.getPartyAlias(party, partyAliasType); 104 } else { // EditMode.UPDATE 105 partyAlias = partyControl.getPartyAliasForUpdate(party, partyAliasType); 106 } 107 108 if(partyAlias != null) { 109 result.setPartyAlias(partyControl.getPartyAliasTransfer(getUserVisit(), partyAlias)); 110 } else { 111 addExecutionError(ExecutionErrors.UnknownPartyAlias.name(), partyName, partyAliasTypeName); 112 } 113 } else { 114 addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), partyType.getPartyTypeName(), partyAliasTypeName); 115 } 116 } else { 117 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 118 } 119 120 return partyAlias; 121 } 122 123 @Override 124 public PartyAlias getLockEntity(PartyAlias partyAlias) { 125 return partyAlias; 126 } 127 128 @Override 129 public void fillInResult(EditPartyAliasResult result, PartyAlias partyAlias) { 130 var partyControl = Session.getModelController(PartyControl.class); 131 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 partyControl = Session.getModelController(PartyControl.class); 143 String alias = edit.getAlias(); 144 PartyAlias duplicatePartyAlias = partyControl.getPartyAliasByAlias(partyAliasType, alias); 145 146 if(duplicatePartyAlias != null && !partyAlias.equals(duplicatePartyAlias)) { 147 PartyAliasTypeDetail partyAliasTypeDetail = partyAlias.getPartyAliasType().getLastDetail(); 148 149 addExecutionError(ExecutionErrors.DuplicatePartyAlias.name(), partyAliasTypeDetail.getPartyType().getPartyTypeName(), 150 partyAliasTypeDetail.getPartyAliasTypeName(), alias); 151 } 152 } 153 154 @Override 155 public void doUpdate(PartyAlias partyAlias) { 156 var partyControl = Session.getModelController(PartyControl.class); 157 PartyAliasValue partyAliasValue = partyControl.getPartyAliasValue(partyAlias); 158 159 partyAliasValue.setAlias(edit.getAlias()); 160 161 partyControl.updatePartyAliasFromValue(partyAliasValue, getPartyPK()); 162 } 163 164}