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.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 com.echothree.util.server.persistence.Session; 044import java.util.Arrays; 045import java.util.Collections; 046import java.util.List; 047import javax.enterprise.context.RequestScoped; 048 049@RequestScoped 050public class CreateContactWebAddressCommand 051 extends BaseSimpleCommand<CreateContactWebAddressForm> { 052 053 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 054 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 060 new PartyTypeDefinition(PartyTypes.VENDOR.name(), null), 061 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 062 new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Create.name()) 063 ))) 064 ))); 065 066 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 068 new FieldDefinition("Url", FieldType.URL, true, null, null), 069 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 070 )); 071 } 072 073 /** Creates a new instance of CreateContactWebAddressCommand */ 074 public CreateContactWebAddressCommand() { 075 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 076 } 077 078 @Override 079 protected SecurityResult security() { 080 var securityResult = super.security(); 081 082 return securityResult != null ? securityResult : selfOnly(form); 083 } 084 085 @Override 086 protected BaseResult execute() { 087 var result = ContactResultFactory.getCreateContactWebAddressResult(); 088 var partyControl = Session.getModelController(PartyControl.class); 089 var partyName = form.getPartyName(); 090 var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName); 091 092 if(party != null) { 093 var contactControl = Session.getModelController(ContactControl.class); 094 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 095 var workflowControl = Session.getModelController(WorkflowControl.class); 096 BasePK createdBy = getPartyPK(); 097 var url = form.getUrl(); 098 var description = form.getDescription(); 099 var contactMechanismName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.CONTACT_MECHANISM.name()); 100 101 var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.WEB_ADDRESS.name()); 102 var contactMechanism = contactControl.createContactMechanism(contactMechanismName, contactMechanismType, 103 false, createdBy); 104 105 contactControl.createContactWebAddress(contactMechanism, url, createdBy); 106 107 contactControl.createPartyContactMechanism(party, contactMechanism, description, false, 1, 108 createdBy); 109 110 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(contactMechanism.getPrimaryKey()); 111 workflowControl.addEntityToWorkflowUsingNames(null, WebAddressStatusConstants.Workflow_WEB_ADDRESS_STATUS, 112 WebAddressStatusConstants.WorkflowEntrance_NEW_WEB_ADDRESS, entityInstance, null, null, createdBy); 113 114 result.setContactMechanismName(contactMechanism.getLastDetail().getContactMechanismName()); 115 result.setEntityRef(contactMechanism.getPrimaryKey().getEntityRef()); 116 } else { 117 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 118 } 119 120 return result; 121 } 122 123}