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.PostalAddressFormatEdit; 021import com.echothree.control.user.contact.common.form.EditPostalAddressFormatForm; 022import com.echothree.control.user.contact.common.result.ContactResultFactory; 023import com.echothree.control.user.contact.common.result.EditPostalAddressFormatResult; 024import com.echothree.control.user.contact.common.spec.PostalAddressFormatSpec; 025import com.echothree.model.control.contact.server.control.ContactControl; 026import com.echothree.model.data.contact.server.entity.PostalAddressFormat; 027import com.echothree.model.data.contact.server.entity.PostalAddressFormatDescription; 028import com.echothree.model.data.contact.server.entity.PostalAddressFormatDetail; 029import com.echothree.model.data.contact.server.value.PostalAddressFormatDescriptionValue; 030import com.echothree.model.data.contact.server.value.PostalAddressFormatDetailValue; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.common.command.BaseResult; 036import com.echothree.util.common.command.EditMode; 037import com.echothree.util.server.control.BaseEditCommand; 038import com.echothree.util.server.persistence.Session; 039import java.util.ArrayList; 040import java.util.Collections; 041import java.util.List; 042 043public class EditPostalAddressFormatCommand 044 extends BaseEditCommand<PostalAddressFormatSpec, PostalAddressFormatEdit> { 045 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 List<FieldDefinition> temp = new ArrayList<>(1); 051 temp.add(new FieldDefinition("PostalAddressFormatName", FieldType.ENTITY_NAME, true, null, null)); 052 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 053 054 temp = new ArrayList<>(4); 055 temp.add(new FieldDefinition("PostalAddressFormatName", FieldType.ENTITY_NAME, true, null, null)); 056 temp.add(new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null)); 057 temp.add(new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)); 058 temp.add(new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)); 059 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 060 } 061 062 /** Creates a new instance of EditPostalAddressFormatCommand */ 063 public EditPostalAddressFormatCommand(UserVisitPK userVisitPK, EditPostalAddressFormatForm form) { 064 super(userVisitPK, form, null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 065 } 066 067 @Override 068 protected BaseResult execute() { 069 var contactControl = Session.getModelController(ContactControl.class); 070 EditPostalAddressFormatResult result = ContactResultFactory.getEditPostalAddressFormatResult(); 071 072 if(editMode.equals(EditMode.LOCK)) { 073 String postalAddressFormatName = spec.getPostalAddressFormatName(); 074 PostalAddressFormat postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName); 075 076 if(postalAddressFormat != null) { 077 result.setPostalAddressFormat(contactControl.getPostalAddressFormatTransfer(getUserVisit(), postalAddressFormat)); 078 079 if(lockEntity(postalAddressFormat)) { 080 PostalAddressFormatDescription postalAddressFormatDescription = contactControl.getPostalAddressFormatDescription(postalAddressFormat, getPreferredLanguage()); 081 PostalAddressFormatEdit edit = ContactEditFactory.getPostalAddressFormatEdit(); 082 PostalAddressFormatDetail postalAddressFormatDetail = postalAddressFormat.getLastDetail(); 083 084 result.setEdit(edit); 085 edit.setPostalAddressFormatName(postalAddressFormatDetail.getPostalAddressFormatName()); 086 edit.setIsDefault(postalAddressFormatDetail.getIsDefault().toString()); 087 edit.setSortOrder(postalAddressFormatDetail.getSortOrder().toString()); 088 089 if(postalAddressFormatDescription != null) 090 edit.setDescription(postalAddressFormatDescription.getDescription()); 091 } else { 092 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 093 } 094 095 result.setEntityLock(getEntityLockTransfer(postalAddressFormat)); 096 } else { 097 addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName); 098 } 099 } else if(editMode.equals(EditMode.UPDATE)) { 100 String postalAddressFormatName = spec.getPostalAddressFormatName(); 101 PostalAddressFormat postalAddressFormat = contactControl.getPostalAddressFormatByNameForUpdate(postalAddressFormatName); 102 103 if(postalAddressFormat != null) { 104 postalAddressFormatName = edit.getPostalAddressFormatName(); 105 PostalAddressFormat duplicatePostalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName); 106 107 if(duplicatePostalAddressFormat == null || postalAddressFormat.equals(duplicatePostalAddressFormat)) { 108 if(lockEntityForUpdate(postalAddressFormat)) { 109 try { 110 var partyPK = getPartyPK(); 111 PostalAddressFormatDetailValue postalAddressFormatDetailValue = contactControl.getPostalAddressFormatDetailValueForUpdate(postalAddressFormat); 112 PostalAddressFormatDescription postalAddressFormatDescription = contactControl.getPostalAddressFormatDescriptionForUpdate(postalAddressFormat, getPreferredLanguage()); 113 String description = edit.getDescription(); 114 115 postalAddressFormatDetailValue.setPostalAddressFormatName(edit.getPostalAddressFormatName()); 116 postalAddressFormatDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 117 postalAddressFormatDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 118 119 contactControl.updatePostalAddressFormatFromValue(postalAddressFormatDetailValue, partyPK); 120 121 if(postalAddressFormatDescription == null && description != null) { 122 contactControl.createPostalAddressFormatDescription(postalAddressFormat, getPreferredLanguage(), description, partyPK); 123 } else if(postalAddressFormatDescription != null && description == null) { 124 contactControl.deletePostalAddressFormatDescription(postalAddressFormatDescription, partyPK); 125 } else if(postalAddressFormatDescription != null && description != null) { 126 PostalAddressFormatDescriptionValue postalAddressFormatDescriptionValue = contactControl.getPostalAddressFormatDescriptionValue(postalAddressFormatDescription); 127 128 postalAddressFormatDescriptionValue.setDescription(description); 129 contactControl.updatePostalAddressFormatDescriptionFromValue(postalAddressFormatDescriptionValue, partyPK); 130 } 131 } finally { 132 unlockEntity(postalAddressFormat); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.EntityLockStale.name()); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.DuplicatePostalAddressFormatName.name(), postalAddressFormatName); 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName); 142 } 143 144 if(hasExecutionErrors()) { 145 result.setPostalAddressFormat(contactControl.getPostalAddressFormatTransfer(getUserVisit(), postalAddressFormat)); 146 result.setEntityLock(getEntityLockTransfer(postalAddressFormat)); 147 } 148 } 149 150 return result; 151 } 152 153}