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.form.CreatePartyContactListForm;
020import com.echothree.control.user.contactlist.common.result.ContactListResultFactory;
021import com.echothree.model.control.contact.server.control.ContactControl;
022import com.echothree.model.control.contactlist.server.control.ContactListControl;
023import com.echothree.model.control.contactlist.server.logic.ContactListLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.contactlist.server.entity.PartyContactList;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class CreatePartyContactListCommand
044        extends BaseSimpleCommand<CreatePartyContactListForm> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.PartyContactList.name(), SecurityRoles.Create.name())
054                ))
055        ));
056
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("PreferredContactMechanismPurposeName", FieldType.ENTITY_NAME, false, null, null)
061        );
062    }
063
064    @Inject
065    ContactControl contactControl;
066
067    @Inject
068    ContactListControl contactListControl;
069
070    @Inject
071    PartyControl partyControl;
072
073    @Inject
074    ContactListLogic contactListLogic;
075
076    /** Creates a new instance of CreatePartyContactListCommand */
077    public CreatePartyContactListCommand() {
078        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
079    }
080    
081    @Override
082    protected BaseResult execute() {
083        var result = ContactListResultFactory.getCreatePartyContactListResult();
084        PartyContactList partyContactList = null;
085        var partyName = form.getPartyName();
086        var party = partyControl.getPartyByName(partyName);
087        
088        if(party != null) {
089            var contactListName = form.getContactListName();
090            var contactList = contactListControl.getContactListByName(contactListName);
091            
092            if(contactList != null) {
093                partyContactList = contactListControl.getPartyContactList(party, contactList);
094                
095                if(partyContactList == null) {
096                    var preferredContactMechanismPurposeName = form.getPreferredContactMechanismPurposeName();
097                    var preferredContactMechanismPurpose = preferredContactMechanismPurposeName == null ? null : contactControl.getContactMechanismPurposeByName(preferredContactMechanismPurposeName);
098
099                    if(preferredContactMechanismPurposeName == null || preferredContactMechanismPurpose != null) {
100                        var preferredContactListContactMechanismPurpose = preferredContactMechanismPurpose == null ? null : contactListControl.getContactListContactMechanismPurpose(contactList, preferredContactMechanismPurpose);
101
102                        if(preferredContactMechanismPurpose == null || preferredContactListContactMechanismPurpose != null) {
103                            // ExecutionErrorAccumulator is passed in as null so that an Exception will be thrown if there is an error.
104                            partyContactList = contactListLogic.addContactListToParty(null, party, contactList, preferredContactListContactMechanismPurpose, getPartyPK());
105                        } else {
106                            addExecutionError(ExecutionErrors.UnknownContactListContactMechanismPurpose.name(), contactListName, preferredContactMechanismPurposeName);
107                        }
108                    } else {
109                        addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), preferredContactMechanismPurposeName);
110                    }
111                } else {
112                    addExecutionError(ExecutionErrors.DuplicatePartyContactList.name(), partyName, contactListName);
113                }
114            } else {
115                addExecutionError(ExecutionErrors.UnknownContactListName.name(), contactListName);
116            }
117        } else {
118            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
119        }
120        
121        if(partyContactList != null) {
122            result.setEntityRef(partyContactList.getPrimaryKey().getEntityRef());
123            result.setPartyName(partyContactList.getLastDetail().getParty().getLastDetail().getPartyName());
124            result.setContactListName(partyContactList.getLastDetail().getContactList().getLastDetail().getContactListName());
125        }
126
127        return result;
128    }
129    
130}