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.WishlistPriorityEdit;
021import com.echothree.control.user.wishlist.common.result.EditWishlistPriorityResult;
022import com.echothree.control.user.wishlist.common.result.WishlistResultFactory;
023import com.echothree.control.user.wishlist.common.spec.WishlistPriorityUniversalSpec;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.control.wishlist.server.control.WishlistControl;
028import com.echothree.model.control.wishlist.server.logic.WishlistPriorityLogic;
029import com.echothree.model.data.wishlist.server.entity.WishlistPriority;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class EditWishlistPriorityCommand
044        extends BaseAbstractEditCommand<WishlistPriorityUniversalSpec, WishlistPriorityEdit, EditWishlistPriorityResult, WishlistPriority, WishlistPriority> {
045
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
048    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.WishlistPriority.name(), SecurityRoles.Edit.name())
055                ))
056        ));
057
058        SPEC_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("WishlistTypeName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("WishlistPriorityName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
062                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
063        );
064        
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("WishlistPriorityName", 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    /** Creates a new instance of EditWishlistPriorityCommand */
074    public EditWishlistPriorityCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077
078    @Inject
079    WishlistControl wishlistControl;
080
081    @Inject
082    WishlistPriorityLogic wishlistPriorityLogic;
083
084    @Override
085    protected EditWishlistPriorityResult getResult() {
086        return WishlistResultFactory.getEditWishlistPriorityResult();
087    }
088
089    @Override
090    protected WishlistPriorityEdit getEdit() {
091        return WishlistEditFactory.getWishlistPriorityEdit();
092    }
093
094    @Override
095    protected WishlistPriority getEntity(EditWishlistPriorityResult result) {
096        WishlistPriority wishlistPriority;
097
098        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
099            wishlistPriority = wishlistPriorityLogic.getWishlistPriorityByUniversalSpec(this, spec, false);
100        } else { // EditMode.UPDATE
101            wishlistPriority = wishlistPriorityLogic.getWishlistPriorityByUniversalSpecForUpdate(this, spec, false);
102        }
103
104        return wishlistPriority;
105    }
106
107    @Override
108    protected WishlistPriority getLockEntity(WishlistPriority wishlistPriority) {
109        return wishlistPriority;
110    }
111
112    @Override
113    protected void fillInResult(EditWishlistPriorityResult result, WishlistPriority wishlistPriority) {
114        result.setWishlistPriority(wishlistControl.getWishlistPriorityTransfer(getUserVisit(), wishlistPriority));
115    }
116
117    @Override
118    protected void doLock(WishlistPriorityEdit edit, WishlistPriority wishlistPriority) {
119        var wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescription(wishlistPriority, getPreferredLanguage());
120        var wishlistPriorityDetail = wishlistPriority.getLastDetail();
121
122        edit.setWishlistPriorityName(wishlistPriorityDetail.getWishlistPriorityName());
123        edit.setIsDefault(wishlistPriorityDetail.getIsDefault().toString());
124        edit.setSortOrder(wishlistPriorityDetail.getSortOrder().toString());
125
126        if(wishlistPriorityDescription != null) {
127            edit.setDescription(wishlistPriorityDescription.getDescription());
128        }
129    }
130
131    @Override
132    protected void canUpdate(WishlistPriority wishlistPriority) {
133        var wishlistType = wishlistPriority.getLastDetail().getWishlistType();
134        var wishlistPriorityName = edit.getWishlistPriorityName();
135        var duplicateWishlistPriority = wishlistControl.getWishlistPriorityByName(wishlistType, wishlistPriorityName);
136
137        if(duplicateWishlistPriority != null && !wishlistPriority.equals(duplicateWishlistPriority)) {
138            addExecutionError(ExecutionErrors.DuplicateWishlistPriorityName.name(), wishlistPriorityName);
139        }
140    }
141
142    @Override
143    protected void doUpdate(WishlistPriority wishlistPriority) {
144        var partyPK = getPartyPK();
145        var wishlistPriorityDetailValue = wishlistControl.getWishlistPriorityDetailValueForUpdate(wishlistPriority);
146        var wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescriptionForUpdate(wishlistPriority, getPreferredLanguage());
147        var description = edit.getDescription();
148
149        wishlistPriorityDetailValue.setWishlistPriorityName(edit.getWishlistPriorityName());
150        wishlistPriorityDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
151        wishlistPriorityDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
152
153        wishlistControl.updateWishlistPriorityFromValue(wishlistPriorityDetailValue, partyPK);
154
155        if(wishlistPriorityDescription == null && description != null) {
156            wishlistControl.createWishlistPriorityDescription(wishlistPriority, getPreferredLanguage(), description, partyPK);
157        } else if(wishlistPriorityDescription != null && description == null) {
158            wishlistControl.deleteWishlistPriorityDescription(wishlistPriorityDescription, partyPK);
159        } else if(wishlistPriorityDescription != null) {
160            var wishlistPriorityDescriptionValue = wishlistControl.getWishlistPriorityDescriptionValue(wishlistPriorityDescription);
161
162            wishlistPriorityDescriptionValue.setDescription(description);
163            wishlistControl.updateWishlistPriorityDescriptionFromValue(wishlistPriorityDescriptionValue, partyPK);
164        }
165    }
166
167}