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    @Inject
074    WishlistControl wishlistControl;
075
076    @Inject
077    WishlistPriorityLogic wishlistPriorityLogic;
078
079
080    /** Creates a new instance of EditWishlistPriorityCommand */
081    public EditWishlistPriorityCommand() {
082        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
083    }
084
085    @Override
086    protected EditWishlistPriorityResult getResult() {
087        return WishlistResultFactory.getEditWishlistPriorityResult();
088    }
089
090    @Override
091    protected WishlistPriorityEdit getEdit() {
092        return WishlistEditFactory.getWishlistPriorityEdit();
093    }
094
095    @Override
096    protected WishlistPriority getEntity(EditWishlistPriorityResult result) {
097        WishlistPriority wishlistPriority;
098
099        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
100            wishlistPriority = wishlistPriorityLogic.getWishlistPriorityByUniversalSpec(this, spec, false);
101        } else { // EditMode.UPDATE
102            wishlistPriority = wishlistPriorityLogic.getWishlistPriorityByUniversalSpecForUpdate(this, spec, false);
103        }
104
105        return wishlistPriority;
106    }
107
108    @Override
109    protected WishlistPriority getLockEntity(WishlistPriority wishlistPriority) {
110        return wishlistPriority;
111    }
112
113    @Override
114    protected void fillInResult(EditWishlistPriorityResult result, WishlistPriority wishlistPriority) {
115        result.setWishlistPriority(wishlistControl.getWishlistPriorityTransfer(getUserVisit(), wishlistPriority));
116    }
117
118    @Override
119    protected void doLock(WishlistPriorityEdit edit, WishlistPriority wishlistPriority) {
120        var wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescription(wishlistPriority, getPreferredLanguage());
121        var wishlistPriorityDetail = wishlistPriority.getLastDetail();
122
123        edit.setWishlistPriorityName(wishlistPriorityDetail.getWishlistPriorityName());
124        edit.setIsDefault(wishlistPriorityDetail.getIsDefault().toString());
125        edit.setSortOrder(wishlistPriorityDetail.getSortOrder().toString());
126
127        if(wishlistPriorityDescription != null) {
128            edit.setDescription(wishlistPriorityDescription.getDescription());
129        }
130    }
131
132    @Override
133    protected void canUpdate(WishlistPriority wishlistPriority) {
134        var wishlistType = wishlistPriority.getLastDetail().getWishlistType();
135        var wishlistPriorityName = edit.getWishlistPriorityName();
136        var duplicateWishlistPriority = wishlistControl.getWishlistPriorityByName(wishlistType, wishlistPriorityName);
137
138        if(duplicateWishlistPriority != null && !wishlistPriority.equals(duplicateWishlistPriority)) {
139            addExecutionError(ExecutionErrors.DuplicateWishlistPriorityName.name(), wishlistPriorityName);
140        }
141    }
142
143    @Override
144    protected void doUpdate(WishlistPriority wishlistPriority) {
145        var partyPK = getPartyPK();
146        var wishlistPriorityDetailValue = wishlistControl.getWishlistPriorityDetailValueForUpdate(wishlistPriority);
147        var wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescriptionForUpdate(wishlistPriority, getPreferredLanguage());
148        var description = edit.getDescription();
149
150        wishlistPriorityDetailValue.setWishlistPriorityName(edit.getWishlistPriorityName());
151        wishlistPriorityDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
152        wishlistPriorityDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
153
154        wishlistControl.updateWishlistPriorityFromValue(wishlistPriorityDetailValue, partyPK);
155
156        if(wishlistPriorityDescription == null && description != null) {
157            wishlistControl.createWishlistPriorityDescription(wishlistPriority, getPreferredLanguage(), description, partyPK);
158        } else if(wishlistPriorityDescription != null && description == null) {
159            wishlistControl.deleteWishlistPriorityDescription(wishlistPriorityDescription, partyPK);
160        } else if(wishlistPriorityDescription != null) {
161            var wishlistPriorityDescriptionValue = wishlistControl.getWishlistPriorityDescriptionValue(wishlistPriorityDescription);
162
163            wishlistPriorityDescriptionValue.setDescription(description);
164            wishlistControl.updateWishlistPriorityDescriptionFromValue(wishlistPriorityDescriptionValue, partyPK);
165        }
166    }
167
168}