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.form.CreateContactListForm; 020import com.echothree.model.control.contactlist.server.ContactListControl; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.security.common.SecurityRoleGroups; 023import com.echothree.model.control.security.common.SecurityRoles; 024import com.echothree.model.control.contactlist.common.workflow.PartyContactListStatusConstants; 025import com.echothree.model.control.workflow.server.control.WorkflowControl; 026import com.echothree.model.data.contactlist.server.entity.ContactList; 027import com.echothree.model.data.contactlist.server.entity.ContactListFrequency; 028import com.echothree.model.data.contactlist.server.entity.ContactListGroup; 029import com.echothree.model.data.contactlist.server.entity.ContactListType; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.model.data.workflow.server.entity.Workflow; 032import com.echothree.model.data.workflow.server.entity.WorkflowEntrance; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.common.command.BaseResult; 037import com.echothree.util.server.control.BaseSimpleCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.Session; 042import java.util.Arrays; 043import java.util.Collections; 044import java.util.List; 045 046public class CreateContactListCommand 047 extends BaseSimpleCommand<CreateContactListForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 056 new SecurityRoleDefinition(SecurityRoleGroups.ContactList.name(), SecurityRoles.Create.name()) 057 ))) 058 ))); 059 060 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 061 new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("ContactListGroupName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("ContactListTypeName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("ContactListFrequencyName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("DefaultPartyContactListStatusChoice", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 067 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 068 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 069 )); 070 } 071 072 /** Creates a new instance of CreateContactListCommand */ 073 public CreateContactListCommand(UserVisitPK userVisitPK, CreateContactListForm form) { 074 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 075 } 076 077 @Override 078 protected BaseResult execute() { 079 var contactListControl = Session.getModelController(ContactListControl.class); 080 String contactListName = form.getContactListName(); 081 ContactList contactList = contactListControl.getContactListByName(contactListName); 082 083 if(contactList == null) { 084 String contactListGroupName = form.getContactListGroupName(); 085 ContactListGroup contactListGroup = contactListControl.getContactListGroupByName(contactListGroupName); 086 087 if(contactListGroup != null) { 088 String contactListTypeName = form.getContactListTypeName(); 089 ContactListType contactListType = contactListControl.getContactListTypeByName(contactListTypeName); 090 091 if(contactListType != null) { 092 String contactListFrequencyName = form.getContactListFrequencyName(); 093 ContactListFrequency contactListFrequency = contactListFrequencyName == null ? null : contactListControl.getContactListFrequencyByName(contactListFrequencyName); 094 095 if(contactListFrequencyName == null || contactListFrequency != null) { 096 var workflowControl = Session.getModelController(WorkflowControl.class); 097 String defaultPartyContactListStatusChoice = form.getDefaultPartyContactListStatusChoice(); 098 var workflow = workflowControl.getWorkflowByName(PartyContactListStatusConstants.Workflow_PARTY_CONTACT_LIST_STATUS); 099 WorkflowEntrance defaultPartyContactListStatus = workflowControl.getWorkflowEntranceByName(workflow, defaultPartyContactListStatusChoice); 100 101 if(defaultPartyContactListStatus != null) { 102 var partyPK = getPartyPK(); 103 var isDefault = Boolean.valueOf(form.getIsDefault()); 104 var sortOrder = Integer.valueOf(form.getSortOrder()); 105 var description = form.getDescription(); 106 107 contactList = contactListControl.createContactList(contactListName, contactListGroup, contactListType, contactListFrequency, 108 defaultPartyContactListStatus, isDefault, sortOrder, partyPK); 109 110 if(description != null) { 111 contactListControl.createContactListDescription(contactList, getPreferredLanguage(), description, partyPK); 112 } 113 } else { 114 addExecutionError(ExecutionErrors.UnknownDefaultPartyContactListStatusChoice.name(), PartyContactListStatusConstants.Workflow_PARTY_CONTACT_LIST_STATUS, 115 defaultPartyContactListStatusChoice); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownContactListFrequencyName.name(), contactListFrequencyName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.UnknownContactListTypeName.name(), contactListTypeName); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownContactListGroupName.name(), contactListGroupName); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.DuplicateContactListName.name(), contactListName); 128 } 129 130 return null; 131 } 132 133}