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