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 com.echothree.util.server.persistence.Session;
033import java.util.ArrayList;
034import java.util.List;
035import javax.enterprise.context.Dependent;
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    /** Creates a new instance of EditPostalAddressLineCommand */
061    public EditPostalAddressLineCommand() {
062        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
063    }
064    
065    @Override
066    protected BaseResult execute() {
067        var contactControl = Session.getModelController(ContactControl.class);
068        var result = ContactResultFactory.getEditPostalAddressLineResult();
069        var postalAddressFormatName = spec.getPostalAddressFormatName();
070        var postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName);
071        
072        if(postalAddressFormat != null) {
073            if(editMode.equals(EditMode.LOCK)) {
074                var postalAddressLineSortOrder = Integer.valueOf(spec.getPostalAddressLineSortOrder());
075                var postalAddressLine = contactControl.getPostalAddressLine(postalAddressFormat, postalAddressLineSortOrder);
076                
077                if(postalAddressLine != null) {
078                    result.setPostalAddressLine(contactControl.getPostalAddressLineTransfer(getUserVisit(), postalAddressLine));
079                    
080                    if(lockEntity(postalAddressLine)) {
081                        var edit = ContactEditFactory.getPostalAddressLineEdit();
082                        var postalAddressLineDetail = postalAddressLine.getLastDetail();
083                        
084                        result.setEdit(edit);
085                        edit.setPostalAddressLineSortOrder(postalAddressLineDetail.getPostalAddressLineSortOrder().toString());
086                        edit.setPrefix(postalAddressLineDetail.getPrefix());
087                        edit.setAlwaysIncludePrefix(postalAddressLineDetail.getAlwaysIncludePrefix().toString());
088                        edit.setSuffix(postalAddressLineDetail.getSuffix());
089                        edit.setAlwaysIncludeSuffix(postalAddressLineDetail.getAlwaysIncludeSuffix().toString());
090                        edit.setCollapseIfEmpty(postalAddressLineDetail.getCollapseIfEmpty().toString());
091                    } else {
092                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
093                    }
094                    
095                    result.setEntityLock(getEntityLockTransfer(postalAddressLine));
096                } else {
097                    addExecutionError(ExecutionErrors.UnknownPostalAddressLine.name(), postalAddressLineSortOrder.toString());
098                }
099            } else if(editMode.equals(EditMode.UPDATE)) {
100                var postalAddressLineSortOrder = Integer.valueOf(spec.getPostalAddressLineSortOrder());
101                var postalAddressLine = contactControl.getPostalAddressLineForUpdate(postalAddressFormat, postalAddressLineSortOrder);
102                
103                if(postalAddressLine != null) {
104                    postalAddressLineSortOrder = Integer.valueOf(edit.getPostalAddressLineSortOrder());
105                    var duplicatePostalAddressLine = contactControl.getPostalAddressLineForUpdate(postalAddressFormat, postalAddressLineSortOrder);
106                    
107                    if(duplicatePostalAddressLine == null || postalAddressLine.equals(duplicatePostalAddressLine)) {
108                        if(lockEntityForUpdate(postalAddressLine)) {
109                            try {
110                                var postalAddressLineDetailValue = contactControl.getPostalAddressLineDetailValueForUpdate(postalAddressLine);
111                                
112                                postalAddressLineDetailValue.setPostalAddressLineSortOrder(Integer.valueOf(edit.getPostalAddressLineSortOrder()));
113                                postalAddressLineDetailValue.setPrefix(edit.getPrefix());
114                                postalAddressLineDetailValue.setAlwaysIncludePrefix(Boolean.valueOf(edit.getAlwaysIncludePrefix()));
115                                postalAddressLineDetailValue.setSuffix(edit.getSuffix());
116                                postalAddressLineDetailValue.setAlwaysIncludeSuffix(Boolean.valueOf(edit.getAlwaysIncludeSuffix()));
117                                postalAddressLineDetailValue.setCollapseIfEmpty(Boolean.valueOf(edit.getCollapseIfEmpty()));
118                                
119                                contactControl.updatePostalAddressLineFromValue(postalAddressLineDetailValue, getPartyPK());
120                            } finally {
121                                unlockEntity(postalAddressLine);
122                            }
123                        } else {
124                            addExecutionError(ExecutionErrors.EntityLockStale.name());
125                        }
126                    } else {
127                        addExecutionError(ExecutionErrors.DuplicatePostalAddressLine.name(), postalAddressLineSortOrder.toString());
128                    }
129                } else {
130                    addExecutionError(ExecutionErrors.UnknownPostalAddressLine.name(), postalAddressLineSortOrder.toString());
131                }
132                
133                if(hasExecutionErrors()) {
134                    result.setPostalAddressLine(contactControl.getPostalAddressLineTransfer(getUserVisit(), postalAddressLine));
135                    result.setEntityLock(getEntityLockTransfer(postalAddressLine));
136                }
137            }
138        } else {
139            addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName);
140        }
141        
142        return result;
143    }
144    
145}