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