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 com.echothree.util.server.persistence.Session; 044import java.util.List; 045import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditContactListCommand */ 074 public EditPartyContactListCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 public EditPartyContactListResult getResult() { 080 return ContactListResultFactory.getEditPartyContactListResult(); 081 } 082 083 @Override 084 public PartyContactListEdit getEdit() { 085 return ContactListEditFactory.getPartyContactListEdit(); 086 } 087 088 ContactList contactList; 089 090 @Override 091 public PartyContactList getEntity(EditPartyContactListResult result) { 092 var partyControl = Session.getModelController(PartyControl.class); 093 PartyContactList partyContactList = null; 094 var partyName = spec.getPartyName(); 095 var party = partyControl.getPartyByName(partyName); 096 097 if(party != null) { 098 var contactListControl = Session.getModelController(ContactListControl.class); 099 var contactListName = spec.getContactListName(); 100 101 contactList = contactListControl.getContactListByName(contactListName); 102 103 if(contactList != null) { 104 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 105 partyContactList = contactListControl.getPartyContactList(party, contactList); 106 } else { // EditMode.UPDATE 107 partyContactList = contactListControl.getPartyContactListForUpdate(party, contactList); 108 } 109 110 if(partyContactList == null) { 111 addExecutionError(ExecutionErrors.UnknownPartyContactList.name(), partyName, contactListName); 112 } 113 } else { 114 addExecutionError(ExecutionErrors.UnknownContactListName.name(), contactListName); 115 } 116 } else { 117 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 118 } 119 120 return partyContactList; 121 } 122 123 @Override 124 public PartyContactList getLockEntity(PartyContactList partyContactList) { 125 return partyContactList; 126 } 127 128 @Override 129 public void fillInResult(EditPartyContactListResult result, PartyContactList partyContactList) { 130 var contactListControl = Session.getModelController(ContactListControl.class); 131 132 result.setPartyContactList(contactListControl.getPartyContactListTransfer(getUserVisit(), partyContactList)); 133 } 134 135 ContactListContactMechanismPurpose preferredContactListContactMechanismPurpose; 136 137 @Override 138 public void doLock(PartyContactListEdit edit, PartyContactList partyContactList) { 139 var partyContactListDetail = partyContactList.getLastDetail(); 140 141 preferredContactListContactMechanismPurpose = partyContactListDetail.getPreferredContactListContactMechanismPurpose(); 142 143 edit.setPreferredContactMechanismPurposeName(preferredContactListContactMechanismPurpose == null ? null : preferredContactListContactMechanismPurpose.getLastDetail().getContactMechanismPurpose().getContactMechanismPurposeName()); 144 } 145 146 @Override 147 public void canUpdate(PartyContactList partyContactList) { 148 var contactControl = Session.getModelController(ContactControl.class); 149 var preferredContactMechanismPurposeName = edit.getPreferredContactMechanismPurposeName(); 150 var preferredContactMechanismPurpose = preferredContactMechanismPurposeName == null ? null : contactControl.getContactMechanismPurposeByName(preferredContactMechanismPurposeName); 151 152 if(preferredContactMechanismPurposeName == null || preferredContactMechanismPurpose != null) { 153 var contactListControl = Session.getModelController(ContactListControl.class); 154 155 preferredContactListContactMechanismPurpose = preferredContactMechanismPurpose == null ? null : contactListControl.getContactListContactMechanismPurpose(contactList, preferredContactMechanismPurpose); 156 157 if(preferredContactMechanismPurpose != null && preferredContactListContactMechanismPurpose == null) { 158 addExecutionError(ExecutionErrors.UnknownContactListContactMechanismPurpose.name(), contactList.getLastDetail().getContactListName(), 159 preferredContactMechanismPurposeName); 160 } 161 } else { 162 addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), preferredContactMechanismPurposeName); 163 } 164 } 165 166 @Override 167 public void doUpdate(PartyContactList partyContactList) { 168 var contactListControl = Session.getModelController(ContactListControl.class); 169 var partyPK = getPartyPK(); 170 var partyContactListDetailValue = contactListControl.getPartyContactListDetailValueForUpdate(partyContactList); 171 172 partyContactListDetailValue.setPreferredContactListContactMechanismPurposePK(preferredContactListContactMechanismPurpose == null ? null : preferredContactListContactMechanismPurpose.getPrimaryKey()); 173 174 contactListControl.updatePartyContactListFromValue(partyContactListDetailValue, partyPK); 175 } 176 177}