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.ContactListEdit; 020import com.echothree.control.user.contactlist.common.edit.ContactListEditFactory; 021import com.echothree.control.user.contactlist.common.form.EditContactListForm; 022import com.echothree.control.user.contactlist.common.result.ContactListResultFactory; 023import com.echothree.control.user.contactlist.common.result.EditContactListResult; 024import com.echothree.control.user.contactlist.common.spec.ContactListSpec; 025import com.echothree.model.control.contactlist.server.control.ContactListControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.control.contactlist.common.workflow.PartyContactListStatusConstants; 030import com.echothree.model.control.workflow.server.control.WorkflowControl; 031import com.echothree.model.data.contactlist.server.entity.ContactList; 032import com.echothree.model.data.contactlist.server.entity.ContactListFrequency; 033import com.echothree.model.data.contactlist.server.entity.ContactListGroup; 034import com.echothree.model.data.contactlist.server.entity.ContactListType; 035import com.echothree.model.data.user.common.pk.UserVisitPK; 036import com.echothree.model.data.workflow.server.entity.WorkflowEntrance; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.common.command.EditMode; 041import com.echothree.util.server.control.BaseAbstractEditCommand; 042import com.echothree.util.server.control.CommandSecurityDefinition; 043import com.echothree.util.server.control.PartyTypeDefinition; 044import com.echothree.util.server.control.SecurityRoleDefinition; 045import com.echothree.util.server.persistence.Session; 046import java.util.List; 047import javax.enterprise.context.Dependent; 048 049@Dependent 050public class EditContactListCommand 051 extends BaseAbstractEditCommand<ContactListSpec, ContactListEdit, EditContactListResult, ContactList, ContactList> { 052 053 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 054 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 055 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 056 057 static { 058 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 059 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 060 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 061 new SecurityRoleDefinition(SecurityRoleGroups.ContactList.name(), SecurityRoles.Edit.name()) 062 )) 063 )); 064 065 SPEC_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null) 067 ); 068 069 EDIT_FIELD_DEFINITIONS = List.of( 070 new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("ContactListGroupName", FieldType.ENTITY_NAME, true, null, null), 072 new FieldDefinition("ContactListTypeName", FieldType.ENTITY_NAME, true, null, null), 073 new FieldDefinition("ContactListFrequencyName", FieldType.ENTITY_NAME, false, null, null), 074 new FieldDefinition("DefaultPartyContactListStatusChoice", FieldType.ENTITY_NAME, true, null, null), 075 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 076 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 077 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 078 ); 079 } 080 081 /** Creates a new instance of EditContactListCommand */ 082 public EditContactListCommand() { 083 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 084 } 085 086 @Override 087 public EditContactListResult getResult() { 088 return ContactListResultFactory.getEditContactListResult(); 089 } 090 091 @Override 092 public ContactListEdit getEdit() { 093 return ContactListEditFactory.getContactListEdit(); 094 } 095 096 @Override 097 public ContactList getEntity(EditContactListResult result) { 098 var contactListControl = Session.getModelController(ContactListControl.class); 099 ContactList contactList; 100 var contactListName = spec.getContactListName(); 101 102 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 103 contactList = contactListControl.getContactListByName(contactListName); 104 } else { // EditMode.UPDATE 105 contactList = contactListControl.getContactListByNameForUpdate(contactListName); 106 } 107 108 if(contactList == null) { 109 addExecutionError(ExecutionErrors.UnknownContactListName.name(), contactListName); 110 } 111 112 return contactList; 113 } 114 115 @Override 116 public ContactList getLockEntity(ContactList contactList) { 117 return contactList; 118 } 119 120 @Override 121 public void fillInResult(EditContactListResult result, ContactList contactList) { 122 var contactListControl = Session.getModelController(ContactListControl.class); 123 124 result.setContactList(contactListControl.getContactListTransfer(getUserVisit(), contactList)); 125 } 126 127 ContactListFrequency contactListFrequency; 128 129 @Override 130 public void doLock(ContactListEdit edit, ContactList contactList) { 131 var contactListControl = Session.getModelController(ContactListControl.class); 132 var contactListDescription = contactListControl.getContactListDescription(contactList, getPreferredLanguage()); 133 var contactListDetail = contactList.getLastDetail(); 134 135 contactListFrequency = contactListDetail.getContactListFrequency(); 136 137 edit.setContactListName(contactListDetail.getContactListName()); 138 edit.setContactListGroupName(contactListDetail.getContactListGroup().getLastDetail().getContactListGroupName()); 139 edit.setContactListTypeName(contactListDetail.getContactListType().getLastDetail().getContactListTypeName()); 140 edit.setContactListFrequencyName(contactListFrequency == null ? null : contactListFrequency.getLastDetail().getContactListFrequencyName()); 141 edit.setDefaultPartyContactListStatusChoice(contactListDetail.getDefaultPartyContactListStatus().getLastDetail().getWorkflowEntranceName()); 142 edit.setIsDefault(contactListDetail.getIsDefault().toString()); 143 edit.setSortOrder(contactListDetail.getSortOrder().toString()); 144 145 if(contactListDescription != null) { 146 edit.setDescription(contactListDescription.getDescription()); 147 } 148 } 149 150 ContactListGroup contactListGroup; 151 ContactListType contactListType; 152 WorkflowEntrance defaultPartyContactListStatus; 153 154 @Override 155 public void canUpdate(ContactList contactList) { 156 var contactListControl = Session.getModelController(ContactListControl.class); 157 var contactListName = edit.getContactListName(); 158 var duplicateContactList = contactListControl.getContactListByName(contactListName); 159 160 if(duplicateContactList != null && !contactList.equals(duplicateContactList)) { 161 addExecutionError(ExecutionErrors.DuplicateContactListName.name(), contactListName); 162 } else { 163 var contactListGroupName = edit.getContactListGroupName(); 164 165 contactListGroup = contactListControl.getContactListGroupByName(contactListGroupName); 166 167 if(contactListGroup != null) { 168 var contactListTypeName = edit.getContactListTypeName(); 169 170 contactListType = contactListControl.getContactListTypeByName(contactListTypeName); 171 172 if(contactListType != null) { 173 var contactListFrequencyName = edit.getContactListFrequencyName(); 174 175 contactListFrequency = contactListFrequencyName == null ? null : contactListControl.getContactListFrequencyByName(contactListFrequencyName); 176 177 if(contactListFrequencyName == null || contactListFrequency != null) { 178 var workflowControl = Session.getModelController(WorkflowControl.class); 179 var defaultPartyContactListStatusChoice = edit.getDefaultPartyContactListStatusChoice(); 180 var workflow = workflowControl.getWorkflowByName(PartyContactListStatusConstants.Workflow_PARTY_CONTACT_LIST_STATUS); 181 182 defaultPartyContactListStatus = workflowControl.getWorkflowEntranceByName(workflow, defaultPartyContactListStatusChoice); 183 184 if(defaultPartyContactListStatus == null) { 185 addExecutionError(ExecutionErrors.UnknownDefaultPartyContactListStatusChoice.name(), PartyContactListStatusConstants.Workflow_PARTY_CONTACT_LIST_STATUS, 186 defaultPartyContactListStatusChoice); 187 } 188 } else { 189 addExecutionError(ExecutionErrors.UnknownContactListFrequencyName.name(), contactListFrequencyName); 190 } 191 } else { 192 addExecutionError(ExecutionErrors.UnknownContactListTypeName.name(), contactListTypeName); 193 } 194 } else { 195 addExecutionError(ExecutionErrors.UnknownContactListGroupName.name(), contactListGroupName); 196 } 197 } 198 } 199 200 @Override 201 public void doUpdate(ContactList contactList) { 202 var contactListControl = Session.getModelController(ContactListControl.class); 203 var partyPK = getPartyPK(); 204 var contactListDetailValue = contactListControl.getContactListDetailValueForUpdate(contactList); 205 var contactListDescription = contactListControl.getContactListDescriptionForUpdate(contactList, getPreferredLanguage()); 206 var description = edit.getDescription(); 207 208 contactListDetailValue.setContactListName(edit.getContactListName()); 209 contactListDetailValue.setContactListGroupPK(contactListGroup.getPrimaryKey()); 210 contactListDetailValue.setContactListTypePK(contactListType.getPrimaryKey()); 211 contactListDetailValue.setContactListFrequencyPK(contactListFrequency == null ? null : contactListFrequency.getPrimaryKey()); 212 contactListDetailValue.setDefaultPartyContactListStatusPK(defaultPartyContactListStatus.getPrimaryKey()); 213 contactListDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 214 contactListDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 215 216 contactListControl.updateContactListFromValue(contactListDetailValue, partyPK); 217 218 if(contactListDescription == null && description != null) { 219 contactListControl.createContactListDescription(contactList, getPreferredLanguage(), description, partyPK); 220 } else if(contactListDescription != null && description == null) { 221 contactListControl.deleteContactListDescription(contactListDescription, partyPK); 222 } else if(contactListDescription != null && description != null) { 223 var contactListDescriptionValue = contactListControl.getContactListDescriptionValue(contactListDescription); 224 225 contactListDescriptionValue.setDescription(description); 226 contactListControl.updateContactListDescriptionFromValue(contactListDescriptionValue, partyPK); 227 } 228 } 229 230}