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.PostalAddressFormatDescriptionEdit;
021import com.echothree.control.user.contact.common.form.EditPostalAddressFormatDescriptionForm;
022import com.echothree.control.user.contact.common.result.ContactResultFactory;
023import com.echothree.control.user.contact.common.result.EditPostalAddressFormatDescriptionResult;
024import com.echothree.control.user.contact.common.spec.PostalAddressFormatDescriptionSpec;
025import com.echothree.model.control.contact.server.control.ContactControl;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.data.contact.server.entity.PostalAddressFormat;
028import com.echothree.model.data.contact.server.entity.PostalAddressFormatDescription;
029import com.echothree.model.data.contact.server.value.PostalAddressFormatDescriptionValue;
030import com.echothree.model.data.party.server.entity.Language;
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 EditPostalAddressFormatDescriptionCommand
044        extends BaseEditCommand<PostalAddressFormatDescriptionSpec, PostalAddressFormatDescriptionEdit> {
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<>(2);
051        temp.add(new FieldDefinition("PostalAddressFormatName", FieldType.ENTITY_NAME, true, null, null));
052        temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null));
053        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp);
054        
055        temp = new ArrayList<>(1);
056        temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L));
057        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp);
058    }
059    
060    /** Creates a new instance of EditPostalAddressFormatDescriptionCommand */
061    public EditPostalAddressFormatDescriptionCommand(UserVisitPK userVisitPK, EditPostalAddressFormatDescriptionForm form) {
062        super(userVisitPK, form, null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
063    }
064    
065    @Override
066    protected BaseResult execute() {
067        var contactControl = Session.getModelController(ContactControl.class);
068        EditPostalAddressFormatDescriptionResult result = ContactResultFactory.getEditPostalAddressFormatDescriptionResult();
069        String postalAddressFormatName = spec.getPostalAddressFormatName();
070        PostalAddressFormat postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName);
071        
072        if(postalAddressFormat != null) {
073            var partyControl = Session.getModelController(PartyControl.class);
074            String languageIsoName = spec.getLanguageIsoName();
075            Language language = partyControl.getLanguageByIsoName(languageIsoName);
076            
077            result.setPostalAddressFormat(contactControl.getPostalAddressFormatTransfer(getUserVisit(), postalAddressFormat));
078            
079            if(language != null) {
080                result.setLanguage(partyControl.getLanguageTransfer(getUserVisit(), language));
081                
082                if(editMode.equals(EditMode.LOCK)) {
083                    PostalAddressFormatDescription postalAddressFormatDescription = contactControl.getPostalAddressFormatDescription(postalAddressFormat, language);
084                    
085                    if(postalAddressFormatDescription != null) {
086                        result.setPostalAddressFormatDescription(contactControl.getPostalAddressFormatDescriptionTransfer(getUserVisit(), postalAddressFormatDescription));
087                        
088                        if(lockEntity(postalAddressFormat)) {
089                            PostalAddressFormatDescriptionEdit edit = ContactEditFactory.getPostalAddressFormatDescriptionEdit();
090                            
091                            result.setEdit(edit);
092                            edit.setDescription(postalAddressFormatDescription.getDescription());
093                        } else {
094                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
095                        }
096                        
097                        result.setEntityLock(getEntityLockTransfer(postalAddressFormat));
098                    } else {
099                        addExecutionError(ExecutionErrors.UnknownPostalAddressFormatDescription.name());
100                    }
101                } else if(editMode.equals(EditMode.UPDATE)) {
102                    PostalAddressFormatDescriptionValue postalAddressFormatDescriptionValue = contactControl.getPostalAddressFormatDescriptionValueForUpdate(postalAddressFormat, language);
103                    
104                    if(postalAddressFormatDescriptionValue != null) {
105                        if(lockEntityForUpdate(postalAddressFormat)) {
106                            try {
107                                String description = edit.getDescription();
108                                
109                                postalAddressFormatDescriptionValue.setDescription(description);
110                                
111                                contactControl.updatePostalAddressFormatDescriptionFromValue(postalAddressFormatDescriptionValue, getPartyPK());
112                            } finally {
113                                unlockEntity(postalAddressFormat);
114                            }
115                        } else {
116                            addExecutionError(ExecutionErrors.EntityLockStale.name());
117                        }
118                    } else {
119                        addExecutionError(ExecutionErrors.UnknownPostalAddressFormatDescription.name());
120                    }
121                }
122            } else {
123                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
124            }
125        } else {
126            addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName);
127        }
128        
129        return result;
130    }
131    
132}