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.offer.server.command;
018
019import com.echothree.control.user.offer.common.edit.OfferEditFactory;
020import com.echothree.control.user.offer.common.edit.UseEdit;
021import com.echothree.control.user.offer.common.form.EditUseForm;
022import com.echothree.control.user.offer.common.result.OfferResultFactory;
023import com.echothree.control.user.offer.common.spec.UseSpec;
024import com.echothree.model.control.offer.server.control.UseControl;
025import com.echothree.model.control.offer.server.control.UseTypeControl;
026import com.echothree.model.control.offer.server.logic.UseLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.command.EditMode;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.Arrays;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class EditUseCommand
048        extends BaseEditCommand<UseSpec, UseEdit> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.Use.name(), SecurityRoles.Edit.name())
059                        )))
060                )));
061        
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null)
064                ));
065        
066        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
067                new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("UseTypeName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
070                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
071                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
072                ));
073    }
074    
075    /** Creates a new instance of EditUseCommand */
076    public EditUseCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079    
080    @Override
081    protected BaseResult execute() {
082        var useControl = Session.getModelController(UseControl.class);
083        var result = OfferResultFactory.getEditUseResult();
084        
085        if(editMode.equals(EditMode.LOCK)) {
086            var useName = spec.getUseName();
087            var use = useControl.getUseByName(useName);
088            
089            if(use != null) {
090                result.setUse(useControl.getUseTransfer(getUserVisit(), use));
091                
092                if(lockEntity(use)) {
093                    var useDescription = useControl.getUseDescription(use, getPreferredLanguage());
094                    var edit = OfferEditFactory.getUseEdit();
095                    var useDetail = use.getLastDetail();
096                    
097                    result.setEdit(edit);
098                    edit.setUseName(useDetail.getUseName());
099                    edit.setUseTypeName(useDetail.getUseType().getLastDetail().getUseTypeName());
100                    edit.setIsDefault(useDetail.getIsDefault().toString());
101                    edit.setSortOrder(useDetail.getSortOrder().toString());
102                    
103                    if(useDescription != null) {
104                        edit.setDescription(useDescription.getDescription());
105                    }
106                } else {
107                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
108                }
109                
110                result.setEntityLock(getEntityLockTransfer(use));
111            } else {
112                addExecutionError(ExecutionErrors.UnknownUseName.name(), useName);
113            }
114        } else if(editMode.equals(EditMode.UPDATE)) {
115            var useName = spec.getUseName();
116            var use = useControl.getUseByNameForUpdate(useName);
117            
118            if(use != null) {
119                useName = edit.getUseName();
120                var duplicateUse = useControl.getUseByName(useName);
121                
122                if(duplicateUse == null || use.equals(duplicateUse)) {
123                    var useTypeControl = Session.getModelController(UseTypeControl.class);
124                    var useTypeName = edit.getUseTypeName();
125                    var useType = useTypeControl.getUseTypeByName(useTypeName);
126                    
127                    if(useType != null) {
128                        if(lockEntityForUpdate(use)) {
129                            try {
130                                var partyPK = getPartyPK();
131                                var useDetailValue = useControl.getUseDetailValueForUpdate(use);
132                                var useDescription = useControl.getUseDescriptionForUpdate(use, getPreferredLanguage());
133                                var description = edit.getDescription();
134                                
135                                useDetailValue.setUseName(edit.getUseName());
136                                useDetailValue.setUseTypePK(useType.getPrimaryKey());
137                                useDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
138                                useDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
139
140                                UseLogic.getInstance().updateUseFromValue(useDetailValue, partyPK);
141                                
142                                if(useDescription == null && description != null) {
143                                    useControl.createUseDescription(use, getPreferredLanguage(), description, partyPK);
144                                } else if(useDescription != null && description == null) {
145                                    useControl.deleteUseDescription(useDescription, partyPK);
146                                } else if(useDescription != null && description != null) {
147                                    var useDescriptionValue = useControl.getUseDescriptionValue(useDescription);
148                                    
149                                    useDescriptionValue.setDescription(description);
150                                    useControl.updateUseDescriptionFromValue(useDescriptionValue, partyPK);
151                                }
152                            } finally {
153                                unlockEntity(use);
154                            }
155                        } else {
156                            addExecutionError(ExecutionErrors.EntityLockStale.name());
157                        }
158                    } else {
159                        addExecutionError(ExecutionErrors.UnknownUseTypeName.name(), useTypeName);
160                    }
161                } else {
162                    addExecutionError(ExecutionErrors.DuplicateUseName.name(), useName);
163                }
164            } else {
165                addExecutionError(ExecutionErrors.UnknownUseName.name(), useName);
166            }
167        }
168        
169        return result;
170    }
171    
172}