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