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