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.contactlist.server.command; 018 019import com.echothree.control.user.contactlist.common.edit.ContactListEditFactory; 020import com.echothree.control.user.contactlist.common.edit.PartyContactListEdit; 021import com.echothree.control.user.contactlist.common.form.EditPartyContactListForm; 022import com.echothree.control.user.contactlist.common.result.ContactListResultFactory; 023import com.echothree.control.user.contactlist.common.result.EditPartyContactListResult; 024import com.echothree.control.user.contactlist.common.spec.PartyContactListSpec; 025import com.echothree.model.control.contact.server.control.ContactControl; 026import com.echothree.model.control.contactlist.server.control.ContactListControl; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.party.server.control.PartyControl; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.data.contactlist.server.entity.ContactList; 032import com.echothree.model.data.contactlist.server.entity.ContactListContactMechanismPurpose; 033import com.echothree.model.data.contactlist.server.entity.PartyContactList; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.common.command.EditMode; 039import com.echothree.util.server.control.BaseAbstractEditCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import java.util.List; 044import javax.enterprise.context.Dependent; 045import javax.inject.Inject; 046 047@Dependent 048public class EditPartyContactListCommand 049 extends BaseAbstractEditCommand<PartyContactListSpec, PartyContactListEdit, EditPartyContactListResult, PartyContactList, PartyContactList> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 059 new SecurityRoleDefinition(SecurityRoleGroups.PartyContactList.name(), SecurityRoles.Edit.name()) 060 )) 061 )); 062 063 SPEC_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("PreferredContactMechanismPurposeName", FieldType.ENTITY_NAME, false, null, null) 070 ); 071 } 072 073 @Inject 074 ContactControl contactControl; 075 076 @Inject 077 ContactListControl contactListControl; 078 079 @Inject 080 PartyControl partyControl; 081 082 /** Creates a new instance of EditContactListCommand */ 083 public EditPartyContactListCommand() { 084 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 085 } 086 087 @Override 088 public EditPartyContactListResult getResult() { 089 return ContactListResultFactory.getEditPartyContactListResult(); 090 } 091 092 @Override 093 public PartyContactListEdit getEdit() { 094 return ContactListEditFactory.getPartyContactListEdit(); 095 } 096 097 ContactList contactList; 098 099 @Override 100 public PartyContactList getEntity(EditPartyContactListResult result) { 101 PartyContactList partyContactList = null; 102 var partyName = spec.getPartyName(); 103 var party = partyControl.getPartyByName(partyName); 104 105 if(party != null) { 106 var contactListName = spec.getContactListName(); 107 108 contactList = contactListControl.getContactListByName(contactListName); 109 110 if(contactList != null) { 111 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 112 partyContactList = contactListControl.getPartyContactList(party, contactList); 113 } else { // EditMode.UPDATE 114 partyContactList = contactListControl.getPartyContactListForUpdate(party, contactList); 115 } 116 117 if(partyContactList == null) { 118 addExecutionError(ExecutionErrors.UnknownPartyContactList.name(), partyName, contactListName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.UnknownContactListName.name(), contactListName); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 125 } 126 127 return partyContactList; 128 } 129 130 @Override 131 public PartyContactList getLockEntity(PartyContactList partyContactList) { 132 return partyContactList; 133 } 134 135 @Override 136 public void fillInResult(EditPartyContactListResult result, PartyContactList partyContactList) { 137 result.setPartyContactList(contactListControl.getPartyContactListTransfer(getUserVisit(), partyContactList)); 138 } 139 140 ContactListContactMechanismPurpose preferredContactListContactMechanismPurpose; 141 142 @Override 143 public void doLock(PartyContactListEdit edit, PartyContactList partyContactList) { 144 var partyContactListDetail = partyContactList.getLastDetail(); 145 146 preferredContactListContactMechanismPurpose = partyContactListDetail.getPreferredContactListContactMechanismPurpose(); 147 148 edit.setPreferredContactMechanismPurposeName(preferredContactListContactMechanismPurpose == null ? null : preferredContactListContactMechanismPurpose.getLastDetail().getContactMechanismPurpose().getContactMechanismPurposeName()); 149 } 150 151 @Override 152 public void canUpdate(PartyContactList partyContactList) { 153 var preferredContactMechanismPurposeName = edit.getPreferredContactMechanismPurposeName(); 154 var preferredContactMechanismPurpose = preferredContactMechanismPurposeName == null ? null : contactControl.getContactMechanismPurposeByName(preferredContactMechanismPurposeName); 155 156 if(preferredContactMechanismPurposeName == null || preferredContactMechanismPurpose != null) { 157 preferredContactListContactMechanismPurpose = preferredContactMechanismPurpose == null ? null : contactListControl.getContactListContactMechanismPurpose(contactList, preferredContactMechanismPurpose); 158 159 if(preferredContactMechanismPurpose != null && preferredContactListContactMechanismPurpose == null) { 160 addExecutionError(ExecutionErrors.UnknownContactListContactMechanismPurpose.name(), contactList.getLastDetail().getContactListName(), 161 preferredContactMechanismPurposeName); 162 } 163 } else { 164 addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), preferredContactMechanismPurposeName); 165 } 166 } 167 168 @Override 169 public void doUpdate(PartyContactList partyContactList) { 170 var partyPK = getPartyPK(); 171 var partyContactListDetailValue = contactListControl.getPartyContactListDetailValueForUpdate(partyContactList); 172 173 partyContactListDetailValue.setPreferredContactListContactMechanismPurposePK(preferredContactListContactMechanismPurpose == null ? null : preferredContactListContactMechanismPurpose.getPrimaryKey()); 174 175 contactListControl.updatePartyContactListFromValue(partyContactListDetailValue, partyPK); 176 } 177 178}