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