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.selector.server.command;
018
019import com.echothree.control.user.selector.common.edit.SelectorEdit;
020import com.echothree.control.user.selector.common.edit.SelectorEditFactory;
021import com.echothree.control.user.selector.common.form.EditSelectorForm;
022import com.echothree.control.user.selector.common.result.EditSelectorResult;
023import com.echothree.control.user.selector.common.result.SelectorResultFactory;
024import com.echothree.control.user.selector.common.spec.SelectorSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.selector.server.control.SelectorControl;
029import com.echothree.model.data.selector.server.entity.Selector;
030import com.echothree.model.data.selector.server.entity.SelectorDescription;
031import com.echothree.model.data.selector.server.entity.SelectorDetail;
032import com.echothree.model.data.selector.server.entity.SelectorKind;
033import com.echothree.model.data.selector.server.entity.SelectorType;
034import com.echothree.model.data.selector.server.value.SelectorDescriptionValue;
035import com.echothree.model.data.selector.server.value.SelectorDetailValue;
036import com.echothree.model.data.user.common.pk.UserVisitPK;
037import com.echothree.util.common.command.BaseResult;
038import com.echothree.util.common.command.EditMode;
039import com.echothree.util.common.message.ExecutionErrors;
040import com.echothree.util.common.validation.FieldDefinition;
041import com.echothree.util.common.validation.FieldType;
042import com.echothree.util.server.control.BaseEditCommand;
043import com.echothree.util.server.control.CommandSecurityDefinition;
044import com.echothree.util.server.control.PartyTypeDefinition;
045import com.echothree.util.server.control.SecurityRoleDefinition;
046import com.echothree.util.server.persistence.Session;
047import java.util.List;
048
049public class EditSelectorCommand
050        extends BaseEditCommand<SelectorSpec, SelectorEdit> {
051
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
054    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
055    
056    static {
057        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
058                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
059                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
060                        new SecurityRoleDefinition(SecurityRoleGroups.Selector.name(), SecurityRoles.Edit.name())
061                ))
062        ));
063
064        SPEC_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null)
068        );
069        
070        EDIT_FIELD_DEFINITIONS = List.of(
071                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("InitialSelectorAdjustmentName", FieldType.ENTITY_NAME, false, null, null),
073                new FieldDefinition("SelectorItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
074                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
075                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
076                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
077        );
078    }
079    
080    /** Creates a new instance of EditSelectorCommand */
081    public EditSelectorCommand(UserVisitPK userVisitPK, EditSelectorForm form) {
082        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
083    }
084    
085    @Override
086    protected BaseResult execute() {
087        var selectorControl = Session.getModelController(SelectorControl.class);
088        EditSelectorResult result = SelectorResultFactory.getEditSelectorResult();
089        String selectorKindName = spec.getSelectorKindName();
090        SelectorKind selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
091        
092        if(selectorKind != null) {
093            String selectorTypeName = spec.getSelectorTypeName();
094            SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
095            
096            if(selectorType != null) {
097                if(editMode.equals(EditMode.LOCK)) {
098                    String selectorName = spec.getSelectorName();
099                    Selector selector = selectorControl.getSelectorByName(selectorType, selectorName);
100                    
101                    if(selector != null) {
102                        result.setSelector(selectorControl.getSelectorTransfer(getUserVisit(), selector));
103                        
104                        if(lockEntity(selector)) {
105                            SelectorDescription selectorDescription = selectorControl.getSelectorDescription(selector, getPreferredLanguage());
106                            SelectorEdit edit = SelectorEditFactory.getSelectorEdit();
107                            SelectorDetail selectorDetail = selector.getLastDetail();
108                            
109                            result.setEdit(edit);
110                            edit.setSelectorName(selectorDetail.getSelectorName());
111                            edit.setIsDefault(selectorDetail.getIsDefault().toString());
112                            edit.setSortOrder(selectorDetail.getSortOrder().toString());
113                            
114                            if(selectorDescription != null) {
115                                edit.setDescription(selectorDescription.getDescription());
116                            }
117                        } else {
118                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
119                        }
120                        
121                        result.setEntityLock(getEntityLockTransfer(selector));
122                    } else {
123                        addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
124                    }
125                } else if(editMode.equals(EditMode.UPDATE)) {
126                    String selectorName = spec.getSelectorName();
127                    Selector selector = selectorControl.getSelectorByNameForUpdate(selectorType, selectorName);
128                    
129                    if(selector != null) {
130                        selectorName = edit.getSelectorName();
131                        Selector duplicateSelector = selectorControl.getSelectorByName(selectorType, selectorName);
132                        
133                        if(duplicateSelector == null || selector.equals(duplicateSelector)) {
134                            if(lockEntityForUpdate(selector)) {
135                                try {
136                                    var partyPK = getPartyPK();
137                                    SelectorDetailValue selectorDetailValue = selectorControl.getSelectorDetailValueForUpdate(selector);
138                                    SelectorDescription selectorDescription = selectorControl.getSelectorDescriptionForUpdate(selector, getPreferredLanguage());
139                                    String description = edit.getDescription();
140                                    
141                                    selectorDetailValue.setSelectorName(edit.getSelectorName());
142                                    selectorDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
143                                    selectorDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
144                                    
145                                    selectorControl.updateSelectorFromValue(selectorDetailValue, partyPK);
146                                    
147                                    if(selectorDescription == null && description != null) {
148                                        selectorControl.createSelectorDescription(selector, getPreferredLanguage(), description, partyPK);
149                                    } else if(selectorDescription != null && description == null) {
150                                        selectorControl.deleteSelectorDescription(selectorDescription, partyPK);
151                                    } else if(selectorDescription != null && description != null) {
152                                        SelectorDescriptionValue selectorDescriptionValue = selectorControl.getSelectorDescriptionValue(selectorDescription);
153                                        
154                                        selectorDescriptionValue.setDescription(description);
155                                        selectorControl.updateSelectorDescriptionFromValue(selectorDescriptionValue, partyPK);
156                                    }
157                                } finally {
158                                    unlockEntity(selector);
159                                }
160                            } else {
161                                addExecutionError(ExecutionErrors.EntityLockStale.name());
162                            }
163                        } else {
164                            addExecutionError(ExecutionErrors.DuplicateSelectorName.name(), selectorName);
165                        }
166                    } else {
167                        addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
168                    }
169                }
170            } else {
171                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
172            }
173        } else {
174            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
175        }
176        
177        return result;
178    }
179    
180}