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.form.GetContactListContactMechanismPurposeForm;
020import com.echothree.control.user.contactlist.common.result.ContactListResultFactory;
021import com.echothree.model.control.contact.server.logic.ContactMechanismPurposeLogic;
022import com.echothree.model.control.contactlist.server.control.ContactListControl;
023import com.echothree.model.control.contactlist.server.logic.ContactListLogic;
024import com.echothree.model.control.core.common.EventTypes;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.server.control.BaseSimpleCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import com.echothree.util.server.persistence.Session;
038import java.util.Arrays;
039import java.util.Collections;
040import java.util.List;
041import javax.enterprise.context.RequestScoped;
042
043@RequestScoped
044public class GetContactListContactMechanismPurposeCommand
045        extends BaseSimpleCommand<GetContactListContactMechanismPurposeForm> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
054                        new SecurityRoleDefinition(SecurityRoleGroups.ContactList.name(), SecurityRoles.ContactListContactMechanismPurpose.name())
055                        )))
056                )));
057        
058        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
059                new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null)
061                ));
062    }
063    
064    /** Creates a new instance of GetContactListContactMechanismPurposeCommand */
065    public GetContactListContactMechanismPurposeCommand() {
066        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var result = ContactListResultFactory.getGetContactListContactMechanismPurposeResult();
072        var contactListName = form.getContactListName();
073        var contactList = ContactListLogic.getInstance().getContactListByName(this, contactListName);
074        
075        if(!hasExecutionErrors()) {
076            var contactMechanismPurposeName = form.getContactMechanismPurposeName();
077            var contactMechanismPurpose = ContactMechanismPurposeLogic.getInstance().getContactMechanismPurposeByName(this, contactMechanismPurposeName);
078            
079            if(!hasExecutionErrors()) {
080                var contactListControl = Session.getModelController(ContactListControl.class);
081                var contactListContactMechanismPurpose = contactListControl.getContactListContactMechanismPurpose(contactList, contactMechanismPurpose);
082                
083                if(contactListContactMechanismPurpose != null) {
084                    result.setContactListContactMechanismPurpose(contactListControl.getContactListContactMechanismPurposeTransfer(getUserVisit(), contactListContactMechanismPurpose));
085
086                    sendEvent(contactListContactMechanismPurpose.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
087                } else {
088                    addExecutionError(ExecutionErrors.DuplicateContactListContactMechanismPurpose.name(), contactListName, contactMechanismPurposeName);
089                }
090            }
091        }
092        
093        return result;
094    }
095    
096}