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.edit.ContactEditFactory; 020import com.echothree.control.user.contact.common.edit.ContactWebAddressEdit; 021import com.echothree.control.user.contact.common.form.EditContactWebAddressForm; 022import com.echothree.control.user.contact.common.result.ContactResultFactory; 023import com.echothree.control.user.contact.common.result.EditContactWebAddressResult; 024import com.echothree.control.user.contact.common.spec.PartyContactMechanismSpec; 025import com.echothree.model.control.contact.common.ContactMechanismTypes; 026import com.echothree.model.control.contact.server.control.ContactControl; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.party.server.control.PartyControl; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.data.contact.server.entity.ContactMechanism; 032import com.echothree.model.data.contact.server.entity.PartyContactMechanism; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.util.common.command.SecurityResult; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.common.command.EditMode; 039import com.echothree.util.server.control.BaseAbstractEditCommand; 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; 047 048public class EditContactWebAddressCommand 049 extends BaseAbstractEditCommand<PartyContactMechanismSpec, ContactWebAddressEdit, EditContactWebAddressResult, PartyContactMechanism, ContactMechanism> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 059 new PartyTypeDefinition(PartyTypes.VENDOR.name(), null), 060 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 061 new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Edit.name()) 062 ))) 063 ))); 064 065 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 066 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 067 new FieldDefinition("ContactMechanismName", FieldType.ENTITY_NAME, true, null, null) 068 )); 069 070 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 071 new FieldDefinition("Url", FieldType.URL, true, null, null), 072 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 073 )); 074 } 075 076 /** Creates a new instance of EditContactWebAddressCommand */ 077 public EditContactWebAddressCommand(UserVisitPK userVisitPK, EditContactWebAddressForm form) { 078 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 protected SecurityResult security() { 083 var securityResult = super.security(); 084 085 return securityResult != null ? securityResult : selfOnly(spec); 086 } 087 088 @Override 089 public EditContactWebAddressResult getResult() { 090 return ContactResultFactory.getEditContactWebAddressResult(); 091 } 092 093 @Override 094 public ContactWebAddressEdit getEdit() { 095 return ContactEditFactory.getContactWebAddressEdit(); 096 } 097 098 @Override 099 public PartyContactMechanism getEntity(EditContactWebAddressResult result) { 100 var partyControl = Session.getModelController(PartyControl.class); 101 PartyContactMechanism partyContactMechanism = null; 102 var partyName = spec.getPartyName(); 103 var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName); 104 105 if(party != null) { 106 var contactControl = Session.getModelController(ContactControl.class); 107 var contactMechanismName = spec.getContactMechanismName(); 108 var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName); 109 110 if(contactMechanism != null) { 111 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 112 partyContactMechanism = contactControl.getPartyContactMechanism(party, contactMechanism); 113 } else { // EditMode.UPDATE 114 partyContactMechanism = contactControl.getPartyContactMechanismForUpdate(party, contactMechanism); 115 } 116 117 if(partyContactMechanism != null) { 118 var lastContactMechanismDetail = contactMechanism.getLastDetail(); 119 var contactMechanismTypeName = lastContactMechanismDetail.getContactMechanismType().getContactMechanismTypeName(); 120 121 result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(), contactMechanism)); 122 123 if(!ContactMechanismTypes.WEB_ADDRESS.name().equals(contactMechanismTypeName)) { 124 addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownPartyContactMechanism.name(), partyName, contactMechanismName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName); 131 } 132 } else { 133 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 134 } 135 136 return partyContactMechanism; 137 } 138 139 @Override 140 public ContactMechanism getLockEntity(PartyContactMechanism partyContactMechanism) { 141 return partyContactMechanism.getLastDetail().getContactMechanism(); 142 } 143 144 @Override 145 public void fillInResult(EditContactWebAddressResult result, PartyContactMechanism partyContactMechanism) { 146 var contactControl = Session.getModelController(ContactControl.class); 147 148 result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(), 149 partyContactMechanism.getLastDetail().getContactMechanism())); 150 } 151 152 @Override 153 public void doLock(ContactWebAddressEdit edit, PartyContactMechanism partyContactMechanism) { 154 var contactControl = Session.getModelController(ContactControl.class); 155 var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism(); 156 var contactWebAddress = contactControl.getContactWebAddress(contactMechanism); 157 var partyContactMechanismDetail = partyContactMechanism.getLastDetail(); 158 159 edit.setUrl(contactWebAddress.getUrl()); 160 edit.setDescription(partyContactMechanismDetail.getDescription()); 161 } 162 163 @Override 164 public void doUpdate(PartyContactMechanism partyContactMechanism) { 165 var contactControl = Session.getModelController(ContactControl.class); 166 var updatedBy = getPartyPK(); 167 var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism(); 168 var contactWebAddressValue = contactControl.getContactWebAddressValueForUpdate(contactMechanism); 169 var partyContactMechanismDetailValue = contactControl.getPartyContactMechanismDetailValueForUpdate(partyContactMechanism); 170 171 contactWebAddressValue.setUrl(edit.getUrl()); 172 partyContactMechanismDetailValue.setDescription(edit.getDescription()); 173 174 contactControl.updateContactWebAddressFromValue(contactWebAddressValue, updatedBy); 175 contactControl.updatePartyContactMechanismFromValue(partyContactMechanismDetailValue, updatedBy); 176 } 177 178} 179