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.wishlist.server.command;
018
019import com.echothree.control.user.wishlist.common.edit.WishlistEditFactory;
020import com.echothree.control.user.wishlist.common.edit.WishlistPriorityDescriptionEdit;
021import com.echothree.control.user.wishlist.common.result.EditWishlistPriorityDescriptionResult;
022import com.echothree.control.user.wishlist.common.result.WishlistResultFactory;
023import com.echothree.control.user.wishlist.common.spec.WishlistPriorityDescriptionSpec;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.logic.LanguageLogic;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.wishlist.server.control.WishlistControl;
029import com.echothree.model.control.wishlist.server.logic.WishlistPriorityLogic;
030import com.echothree.model.control.wishlist.server.logic.WishlistTypeLogic;
031import com.echothree.model.data.wishlist.server.entity.WishlistPriority;
032import com.echothree.model.data.wishlist.server.entity.WishlistPriorityDescription;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditWishlistPriorityDescriptionCommand
045        extends BaseAbstractEditCommand<WishlistPriorityDescriptionSpec, WishlistPriorityDescriptionEdit, EditWishlistPriorityDescriptionResult, WishlistPriorityDescription, WishlistPriority> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.WishlistPriority.name(), SecurityRoles.Description.name())
056                ))
057        ));
058
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("WishlistTypeName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("WishlistPriorityName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
063        );
064        
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
067        );
068    }
069
070    @Inject
071    WishlistControl wishlistControl;
072
073    @Inject
074    LanguageLogic languageLogic;
075
076    @Inject
077    WishlistPriorityLogic wishlistPriorityLogic;
078
079    @Inject
080    WishlistTypeLogic wishlistTypeLogic;
081
082
083    /** Creates a new instance of EditWishlistPriorityDescriptionCommand */
084    public EditWishlistPriorityDescriptionCommand() {
085        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
086    }
087
088    @Override
089    public EditWishlistPriorityDescriptionResult getResult() {
090        return WishlistResultFactory.getEditWishlistPriorityDescriptionResult();
091    }
092
093    @Override
094    public WishlistPriorityDescriptionEdit getEdit() {
095        return WishlistEditFactory.getWishlistPriorityDescriptionEdit();
096    }
097
098    @Override
099    public WishlistPriorityDescription getEntity(EditWishlistPriorityDescriptionResult result) {
100        var wishlistTypeName = spec.getWishlistTypeName();
101        var wishlistType = wishlistTypeLogic.getWishlistTypeByName(this, wishlistTypeName);
102        WishlistPriorityDescription wishlistPriorityDescription = null;
103
104        if(!hasExecutionErrors()) {
105            var wishlistPriorityName = spec.getWishlistPriorityName();
106            var wishlistPriority = wishlistPriorityLogic.getWishlistPriorityByName(this, wishlistType, wishlistPriorityName);
107
108            if(!hasExecutionErrors()) {
109                var languageIsoName = spec.getLanguageIsoName();
110                var language = languageLogic.getLanguageByName(this, languageIsoName);
111
112                if(!hasExecutionErrors()) {
113                    if(editMode.equals(com.echothree.util.common.command.EditMode.LOCK) || editMode.equals(com.echothree.util.common.command.EditMode.ABANDON)) {
114                        wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescription(wishlistPriority, language);
115                    } else {
116                        wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescriptionForUpdate(wishlistPriority, language);
117                    }
118
119                    if(wishlistPriorityDescription == null) {
120                        addExecutionError(com.echothree.util.common.message.ExecutionErrors.UnknownWishlistPriorityDescription.name(), wishlistTypeName, wishlistPriorityName, languageIsoName);
121                    }
122                }
123            }
124        }
125
126        return wishlistPriorityDescription;
127    }
128
129    @Override
130    public WishlistPriority getLockEntity(WishlistPriorityDescription wishlistPriorityDescription) {
131        return wishlistPriorityDescription.getWishlistPriority();
132    }
133
134    @Override
135    public void fillInResult(EditWishlistPriorityDescriptionResult result, WishlistPriorityDescription wishlistPriorityDescription) {
136        result.setWishlistPriorityDescription(wishlistControl.getWishlistPriorityDescriptionTransfer(getUserVisit(), wishlistPriorityDescription));
137    }
138
139    @Override
140    public void doLock(WishlistPriorityDescriptionEdit edit, WishlistPriorityDescription wishlistPriorityDescription) {
141        edit.setDescription(wishlistPriorityDescription.getDescription());
142    }
143
144    @Override
145    public void doUpdate(WishlistPriorityDescription wishlistPriorityDescription) {
146        var wishlistPriorityDescriptionValue = wishlistControl.getWishlistPriorityDescriptionValue(wishlistPriorityDescription);
147
148        wishlistPriorityDescriptionValue.setDescription(edit.getDescription());
149
150        wishlistControl.updateWishlistPriorityDescriptionFromValue(wishlistPriorityDescriptionValue, getPartyPK());
151    }
152
153}