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.edit.ContactEditFactory;
020import com.echothree.control.user.contact.common.edit.PostalAddressLineEdit;
021import com.echothree.control.user.contact.common.form.EditPostalAddressLineForm;
022import com.echothree.control.user.contact.common.result.ContactResultFactory;
023import com.echothree.control.user.contact.common.spec.PostalAddressLineSpec;
024import com.echothree.model.control.contact.server.control.ContactControl;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.server.control.BaseEditCommand;
032import java.util.ArrayList;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class EditPostalAddressLineCommand
039        extends BaseEditCommand<PostalAddressLineSpec, PostalAddressLineEdit> {
040    
041    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
042    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
043    
044    static {
045        SPEC_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("PostalAddressFormatName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("PostalAddressLineSortOrder", FieldType.SIGNED_INTEGER, true, null, null)
048        );
049        
050        EDIT_FIELD_DEFINITIONS = List.of(
051                new FieldDefinition("PostalAddressLineSortOrder", FieldType.SIGNED_INTEGER, true, null, null),
052                new FieldDefinition("Prefix", FieldType.STRING, false, 1L, 10L),
053                new FieldDefinition("AlwaysIncludePrefix", FieldType.BOOLEAN, true, null, null),
054                new FieldDefinition("Suffix", FieldType.STRING, false, 1L, 10L),
055                new FieldDefinition("AlwaysIncludeSuffix", FieldType.BOOLEAN, true, null, null),
056                new FieldDefinition("CollapseIfEmpty", FieldType.BOOLEAN, true, null, null)
057        );
058    }
059
060    @Inject
061    ContactControl contactControl;
062
063    
064    /** Creates a new instance of EditPostalAddressLineCommand */
065    public EditPostalAddressLineCommand() {
066        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var result = ContactResultFactory.getEditPostalAddressLineResult();
072        var postalAddressFormatName = spec.getPostalAddressFormatName();
073        var postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName);
074        
075        if(postalAddressFormat != null) {
076            if(editMode.equals(EditMode.LOCK)) {
077                var postalAddressLineSortOrder = Integer.valueOf(spec.getPostalAddressLineSortOrder());
078                var postalAddressLine = contactControl.getPostalAddressLine(postalAddressFormat, postalAddressLineSortOrder);
079                
080                if(postalAddressLine != null) {
081                    result.setPostalAddressLine(contactControl.getPostalAddressLineTransfer(getUserVisit(), postalAddressLine));
082                    
083                    if(lockEntity(postalAddressLine)) {
084                        var edit = ContactEditFactory.getPostalAddressLineEdit();
085                        var postalAddressLineDetail = postalAddressLine.getLastDetail();
086                        
087                        result.setEdit(edit);
088                        edit.setPostalAddressLineSortOrder(postalAddressLineDetail.getPostalAddressLineSortOrder().toString());
089                        edit.setPrefix(postalAddressLineDetail.getPrefix());
090                        edit.setAlwaysIncludePrefix(postalAddressLineDetail.getAlwaysIncludePrefix().toString());
091                        edit.setSuffix(postalAddressLineDetail.getSuffix());
092                        edit.setAlwaysIncludeSuffix(postalAddressLineDetail.getAlwaysIncludeSuffix().toString());
093                        edit.setCollapseIfEmpty(postalAddressLineDetail.getCollapseIfEmpty().toString());
094                    } else {
095                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
096                    }
097                    
098                    result.setEntityLock(getEntityLockTransfer(postalAddressLine));
099                } else {
100                    addExecutionError(ExecutionErrors.UnknownPostalAddressLine.name(), postalAddressLineSortOrder.toString());
101                }
102            } else if(editMode.equals(EditMode.UPDATE)) {
103                var postalAddressLineSortOrder = Integer.valueOf(spec.getPostalAddressLineSortOrder());
104                var postalAddressLine = contactControl.getPostalAddressLineForUpdate(postalAddressFormat, postalAddressLineSortOrder);
105                
106                if(postalAddressLine != null) {
107                    postalAddressLineSortOrder = Integer.valueOf(edit.getPostalAddressLineSortOrder());
108                    var duplicatePostalAddressLine = contactControl.getPostalAddressLineForUpdate(postalAddressFormat, postalAddressLineSortOrder);
109                    
110                    if(duplicatePostalAddressLine == null || postalAddressLine.equals(duplicatePostalAddressLine)) {
111                        if(lockEntityForUpdate(postalAddressLine)) {
112                            try {
113                                var postalAddressLineDetailValue = contactControl.getPostalAddressLineDetailValueForUpdate(postalAddressLine);
114                                
115                                postalAddressLineDetailValue.setPostalAddressLineSortOrder(Integer.valueOf(edit.getPostalAddressLineSortOrder()));
116                                postalAddressLineDetailValue.setPrefix(edit.getPrefix());
117                                postalAddressLineDetailValue.setAlwaysIncludePrefix(Boolean.valueOf(edit.getAlwaysIncludePrefix()));
118                                postalAddressLineDetailValue.setSuffix(edit.getSuffix());
119                                postalAddressLineDetailValue.setAlwaysIncludeSuffix(Boolean.valueOf(edit.getAlwaysIncludeSuffix()));
120                                postalAddressLineDetailValue.setCollapseIfEmpty(Boolean.valueOf(edit.getCollapseIfEmpty()));
121                                
122                                contactControl.updatePostalAddressLineFromValue(postalAddressLineDetailValue, getPartyPK());
123                            } finally {
124                                unlockEntity(postalAddressLine);
125                            }
126                        } else {
127                            addExecutionError(ExecutionErrors.EntityLockStale.name());
128                        }
129                    } else {
130                        addExecutionError(ExecutionErrors.DuplicatePostalAddressLine.name(), postalAddressLineSortOrder.toString());
131                    }
132                } else {
133                    addExecutionError(ExecutionErrors.UnknownPostalAddressLine.name(), postalAddressLineSortOrder.toString());
134                }
135                
136                if(hasExecutionErrors()) {
137                    result.setPostalAddressLine(contactControl.getPostalAddressLineTransfer(getUserVisit(), postalAddressLine));
138                    result.setEntityLock(getEntityLockTransfer(postalAddressLine));
139                }
140            }
141        } else {
142            addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName);
143        }
144        
145        return result;
146    }
147    
148}