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.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.List;
039import javax.enterprise.context.Dependent;
040
041@Dependent
042public class GetContactListContactMechanismPurposeCommand
043        extends BaseSimpleCommand<GetContactListContactMechanismPurposeForm> {
044    
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047    
048    static {
049        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
050                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
051                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
052                        new SecurityRoleDefinition(SecurityRoleGroups.ContactList.name(), SecurityRoles.ContactListContactMechanismPurpose.name())
053                        ))
054                ));
055        
056        FORM_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null)
059                );
060    }
061    
062    /** Creates a new instance of GetContactListContactMechanismPurposeCommand */
063    public GetContactListContactMechanismPurposeCommand() {
064        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
065    }
066    
067    @Override
068    protected BaseResult execute() {
069        var result = ContactListResultFactory.getGetContactListContactMechanismPurposeResult();
070        var contactListName = form.getContactListName();
071        var contactList = ContactListLogic.getInstance().getContactListByName(this, contactListName);
072        
073        if(!hasExecutionErrors()) {
074            var contactMechanismPurposeName = form.getContactMechanismPurposeName();
075            var contactMechanismPurpose = ContactMechanismPurposeLogic.getInstance().getContactMechanismPurposeByName(this, contactMechanismPurposeName);
076            
077            if(!hasExecutionErrors()) {
078                var contactListControl = Session.getModelController(ContactListControl.class);
079                var contactListContactMechanismPurpose = contactListControl.getContactListContactMechanismPurpose(contactList, contactMechanismPurpose);
080                
081                if(contactListContactMechanismPurpose != null) {
082                    result.setContactListContactMechanismPurpose(contactListControl.getContactListContactMechanismPurposeTransfer(getUserVisit(), contactListContactMechanismPurpose));
083
084                    sendEvent(contactListContactMechanismPurpose.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
085                } else {
086                    addExecutionError(ExecutionErrors.DuplicateContactListContactMechanismPurpose.name(), contactListName, contactMechanismPurposeName);
087                }
088            }
089        }
090        
091        return result;
092    }
093    
094}