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