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    WishlistPriorityLogic wishlistPriorityLogic;
075
076    @Inject
077    LanguageLogic languageLogic;
078
079    @Inject
080    WishlistTypeLogic wishlistTypeLogic;
081
082    /** Creates a new instance of EditWishlistPriorityDescriptionCommand */
083    public EditWishlistPriorityDescriptionCommand() {
084        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
085    }
086
087    @Override
088    public EditWishlistPriorityDescriptionResult getResult() {
089        return WishlistResultFactory.getEditWishlistPriorityDescriptionResult();
090    }
091
092    @Override
093    public WishlistPriorityDescriptionEdit getEdit() {
094        return WishlistEditFactory.getWishlistPriorityDescriptionEdit();
095    }
096
097    @Override
098    public WishlistPriorityDescription getEntity(EditWishlistPriorityDescriptionResult result) {
099        var wishlistTypeName = spec.getWishlistTypeName();
100        var wishlistType = wishlistTypeLogic.getWishlistTypeByName(this, wishlistTypeName);
101        WishlistPriorityDescription wishlistPriorityDescription = null;
102
103        if(!hasExecutionErrors()) {
104            var wishlistPriorityName = spec.getWishlistPriorityName();
105            var wishlistPriority = wishlistPriorityLogic.getWishlistPriorityByName(this, wishlistType, wishlistPriorityName);
106
107            if(!hasExecutionErrors()) {
108                var languageIsoName = spec.getLanguageIsoName();
109                var language = languageLogic.getLanguageByName(this, languageIsoName);
110
111                if(!hasExecutionErrors()) {
112                    if(editMode.equals(com.echothree.util.common.command.EditMode.LOCK) || editMode.equals(com.echothree.util.common.command.EditMode.ABANDON)) {
113                        wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescription(wishlistPriority, language);
114                    } else {
115                        wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescriptionForUpdate(wishlistPriority, language);
116                    }
117
118                    if(wishlistPriorityDescription == null) {
119                        addExecutionError(com.echothree.util.common.message.ExecutionErrors.UnknownWishlistPriorityDescription.name(), wishlistTypeName, wishlistPriorityName, languageIsoName);
120                    }
121                }
122            }
123        }
124
125        return wishlistPriorityDescription;
126    }
127
128    @Override
129    public WishlistPriority getLockEntity(WishlistPriorityDescription wishlistPriorityDescription) {
130        return wishlistPriorityDescription.getWishlistPriority();
131    }
132
133    @Override
134    public void fillInResult(EditWishlistPriorityDescriptionResult result, WishlistPriorityDescription wishlistPriorityDescription) {
135        result.setWishlistPriorityDescription(wishlistControl.getWishlistPriorityDescriptionTransfer(getUserVisit(), wishlistPriorityDescription));
136    }
137
138    @Override
139    public void doLock(WishlistPriorityDescriptionEdit edit, WishlistPriorityDescription wishlistPriorityDescription) {
140        edit.setDescription(wishlistPriorityDescription.getDescription());
141    }
142
143    @Override
144    public void doUpdate(WishlistPriorityDescription wishlistPriorityDescription) {
145        var wishlistPriorityDescriptionValue = wishlistControl.getWishlistPriorityDescriptionValue(wishlistPriorityDescription);
146
147        wishlistPriorityDescriptionValue.setDescription(edit.getDescription());
148
149        wishlistControl.updateWishlistPriorityDescriptionFromValue(wishlistPriorityDescriptionValue, getPartyPK());
150    }
151
152}