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.contact.server.command;
018
019import com.echothree.control.user.contact.common.form.CreateContactWebAddressForm;
020import com.echothree.control.user.contact.common.result.ContactResultFactory;
021import com.echothree.model.control.contact.common.ContactMechanismTypes;
022import com.echothree.model.control.contact.common.workflow.WebAddressStatusConstants;
023import com.echothree.model.control.contact.server.control.ContactControl;
024import com.echothree.model.control.core.server.control.EntityInstanceControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.control.sequence.common.SequenceTypes;
030import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic;
031import com.echothree.model.control.workflow.server.control.WorkflowControl;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.command.SecurityResult;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.persistence.BasePK;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.server.control.BaseSimpleCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import java.util.List;
044import javax.enterprise.context.Dependent;
045import javax.inject.Inject;
046
047@Dependent
048public class CreateContactWebAddressCommand
049        extends BaseSimpleCommand<CreateContactWebAddressForm> {
050
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
053
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
058                new PartyTypeDefinition(PartyTypes.VENDOR.name(), null),
059                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
060                        new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Create.name())
061                ))
062        ));
063
064        FORM_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("Url", FieldType.URL, true, null, null),
067                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
068        );
069    }
070
071    @Inject
072    ContactControl contactControl;
073
074    @Inject
075    EntityInstanceControl entityInstanceControl;
076
077    @Inject
078    PartyControl partyControl;
079
080    @Inject
081    WorkflowControl workflowControl;
082
083    @Inject
084    SequenceGeneratorLogic sequenceGeneratorLogic;
085
086    
087    /** Creates a new instance of CreateContactWebAddressCommand */
088    public CreateContactWebAddressCommand() {
089        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
090    }
091
092    @Override
093    protected SecurityResult security() {
094        var securityResult = super.security();
095
096        return securityResult != null ? securityResult : selfOnly(form);
097    }
098
099    @Override
100    protected BaseResult execute() {
101        var result = ContactResultFactory.getCreateContactWebAddressResult();
102        var partyName = form.getPartyName();
103        var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName);
104        
105        if(party != null) {
106            BasePK createdBy = getPartyPK();
107            var url = form.getUrl();
108            var description = form.getDescription();
109            var contactMechanismName = sequenceGeneratorLogic.getNextSequenceValue(null, SequenceTypes.CONTACT_MECHANISM.name());
110
111            var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.WEB_ADDRESS.name());
112            var contactMechanism = contactControl.createContactMechanism(contactMechanismName, contactMechanismType,
113                    false, createdBy);
114            
115            contactControl.createContactWebAddress(contactMechanism, url, createdBy);
116            
117            contactControl.createPartyContactMechanism(party, contactMechanism, description, false, 1,
118                    createdBy);
119
120            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(contactMechanism.getPrimaryKey());
121            workflowControl.addEntityToWorkflowUsingNames(null, WebAddressStatusConstants.Workflow_WEB_ADDRESS_STATUS,
122                    WebAddressStatusConstants.WorkflowEntrance_NEW_WEB_ADDRESS, entityInstance, null, null, createdBy);
123            
124            result.setContactMechanismName(contactMechanism.getLastDetail().getContactMechanismName());
125            result.setEntityRef(contactMechanism.getPrimaryKey().getEntityRef());
126        } else {
127            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
128        }
129        
130        return result;
131    }
132    
133}