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 com.echothree.util.server.persistence.Session; 044import java.util.List; 045import javax.enterprise.context.Dependent; 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 /** Creates a new instance of CreateContactWebAddressCommand */ 072 public CreateContactWebAddressCommand() { 073 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 074 } 075 076 @Override 077 protected SecurityResult security() { 078 var securityResult = super.security(); 079 080 return securityResult != null ? securityResult : selfOnly(form); 081 } 082 083 @Override 084 protected BaseResult execute() { 085 var result = ContactResultFactory.getCreateContactWebAddressResult(); 086 var partyControl = Session.getModelController(PartyControl.class); 087 var partyName = form.getPartyName(); 088 var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName); 089 090 if(party != null) { 091 var contactControl = Session.getModelController(ContactControl.class); 092 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 093 var workflowControl = Session.getModelController(WorkflowControl.class); 094 BasePK createdBy = getPartyPK(); 095 var url = form.getUrl(); 096 var description = form.getDescription(); 097 var contactMechanismName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.CONTACT_MECHANISM.name()); 098 099 var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.WEB_ADDRESS.name()); 100 var contactMechanism = contactControl.createContactMechanism(contactMechanismName, contactMechanismType, 101 false, createdBy); 102 103 contactControl.createContactWebAddress(contactMechanism, url, createdBy); 104 105 contactControl.createPartyContactMechanism(party, contactMechanism, description, false, 1, 106 createdBy); 107 108 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(contactMechanism.getPrimaryKey()); 109 workflowControl.addEntityToWorkflowUsingNames(null, WebAddressStatusConstants.Workflow_WEB_ADDRESS_STATUS, 110 WebAddressStatusConstants.WorkflowEntrance_NEW_WEB_ADDRESS, entityInstance, null, null, createdBy); 111 112 result.setContactMechanismName(contactMechanism.getLastDetail().getContactMechanismName()); 113 result.setEntityRef(contactMechanism.getPrimaryKey().getEntityRef()); 114 } else { 115 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 116 } 117 118 return result; 119 } 120 121}