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