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.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.form.EditWishlistPriorityForm;
022import com.echothree.control.user.wishlist.common.result.EditWishlistPriorityResult;
023import com.echothree.control.user.wishlist.common.result.WishlistResultFactory;
024import com.echothree.control.user.wishlist.common.spec.WishlistPriorityUniversalSpec;
025import com.echothree.model.control.party.common.PartyTypes;
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.data.user.common.pk.UserVisitPK;
031import com.echothree.model.data.wishlist.server.entity.WishlistPriority;
032import com.echothree.model.data.wishlist.server.entity.WishlistPriorityDescription;
033import com.echothree.model.data.wishlist.server.entity.WishlistPriorityDetail;
034import com.echothree.model.data.wishlist.server.value.WishlistPriorityDescriptionValue;
035import com.echothree.model.data.wishlist.server.value.WishlistPriorityDetailValue;
036import com.echothree.util.common.command.BaseResult;
037import com.echothree.util.common.command.EditMode;
038import com.echothree.util.common.message.ExecutionErrors;
039import com.echothree.util.common.validation.FieldDefinition;
040import com.echothree.util.common.validation.FieldType;
041import com.echothree.util.server.control.BaseEditCommand;
042import com.echothree.util.server.control.CommandSecurityDefinition;
043import com.echothree.util.server.control.PartyTypeDefinition;
044import com.echothree.util.server.control.SecurityRoleDefinition;
045import com.echothree.util.server.persistence.Session;
046import java.util.Arrays;
047import java.util.Collections;
048import java.util.List;
049
050public class EditWishlistPriorityCommand
051        extends BaseEditCommand<WishlistPriorityUniversalSpec, WishlistPriorityEdit> {
052
053    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
054    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
055    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
056    
057    static {
058        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
061                        new SecurityRoleDefinition(SecurityRoleGroups.WishlistPriority.name(), SecurityRoles.Edit.name())
062                )))
063        )));
064
065        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("WishlistTypeName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("WishlistPriorityName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
069                new FieldDefinition("Key", FieldType.KEY, false, null, null),
070                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
071                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
072                ));
073        
074        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
075                new FieldDefinition("WishlistPriorityName", FieldType.ENTITY_NAME, true, null, null),
076                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
077                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
078                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
079                ));
080    }
081    
082    /** Creates a new instance of EditWishlistPriorityCommand */
083    public EditWishlistPriorityCommand(UserVisitPK userVisitPK, EditWishlistPriorityForm form) {
084        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
085    }
086    
087    @Override
088    protected BaseResult execute() {
089        var wishlistControl = Session.getModelController(WishlistControl.class);
090        EditWishlistPriorityResult result = WishlistResultFactory.getEditWishlistPriorityResult();
091        
092        if(editMode.equals(EditMode.LOCK)) {
093            var wishlistPriority = WishlistPriorityLogic.getInstance().getWishlistPriorityByUniversalSpec(this, spec, false);
094
095            if(!hasExecutionErrors()) {
096                result.setWishlistPriority(wishlistControl.getWishlistPriorityTransfer(getUserVisit(), wishlistPriority));
097
098                if(lockEntity(wishlistPriority)) {
099                    WishlistPriorityDescription wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescription(wishlistPriority, getPreferredLanguage());
100                    WishlistPriorityEdit edit = WishlistEditFactory.getWishlistPriorityEdit();
101                    WishlistPriorityDetail wishlistPriorityDetail = wishlistPriority.getLastDetail();
102
103                    result.setEdit(edit);
104                    edit.setWishlistPriorityName(wishlistPriorityDetail.getWishlistPriorityName());
105                    edit.setIsDefault(wishlistPriorityDetail.getIsDefault().toString());
106                    edit.setSortOrder(wishlistPriorityDetail.getSortOrder().toString());
107
108                    if(wishlistPriorityDescription != null)
109                        edit.setDescription(wishlistPriorityDescription.getDescription());
110                } else {
111                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
112                }
113
114                result.setEntityLock(getEntityLockTransfer(wishlistPriority));
115            }
116        } else if(editMode.equals(EditMode.UPDATE)) {
117            var wishlistPriority = WishlistPriorityLogic.getInstance().getWishlistPriorityByUniversalSpecForUpdate(this, spec, false);
118                
119            if(wishlistPriority != null) {
120                var wishlistType = wishlistPriority.getLastDetail().getWishlistType();
121                var wishlistPriorityName = edit.getWishlistPriorityName();
122                WishlistPriority duplicateWishlistPriority = wishlistControl.getWishlistPriorityByName(wishlistType, wishlistPriorityName);
123
124                if(duplicateWishlistPriority == null || wishlistPriority.equals(duplicateWishlistPriority)) {
125                    if(lockEntityForUpdate(wishlistPriority)) {
126                        try {
127                            var partyPK = getPartyPK();
128                            WishlistPriorityDetailValue wishlistPriorityDetailValue = wishlistControl.getWishlistPriorityDetailValueForUpdate(wishlistPriority);
129                            WishlistPriorityDescription wishlistPriorityDescription = wishlistControl.getWishlistPriorityDescriptionForUpdate(wishlistPriority, getPreferredLanguage());
130                            String description = edit.getDescription();
131
132                            wishlistPriorityDetailValue.setWishlistPriorityName(edit.getWishlistPriorityName());
133                            wishlistPriorityDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
134                            wishlistPriorityDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
135
136                            wishlistControl.updateWishlistPriorityFromValue(wishlistPriorityDetailValue, partyPK);
137
138                            if(wishlistPriorityDescription == null && description != null) {
139                                wishlistControl.createWishlistPriorityDescription(wishlistPriority, getPreferredLanguage(), description, partyPK);
140                            } else if(wishlistPriorityDescription != null && description == null) {
141                                wishlistControl.deleteWishlistPriorityDescription(wishlistPriorityDescription, partyPK);
142                            } else if(wishlistPriorityDescription != null && description != null) {
143                                WishlistPriorityDescriptionValue wishlistPriorityDescriptionValue = wishlistControl.getWishlistPriorityDescriptionValue(wishlistPriorityDescription);
144
145                                wishlistPriorityDescriptionValue.setDescription(description);
146                                wishlistControl.updateWishlistPriorityDescriptionFromValue(wishlistPriorityDescriptionValue, partyPK);
147                            }
148                        } finally {
149                            unlockEntity(wishlistPriority);
150                        }
151                    } else {
152                        addExecutionError(ExecutionErrors.EntityLockStale.name());
153                    }
154                } else {
155                    addExecutionError(ExecutionErrors.DuplicateWishlistPriorityName.name(), wishlistPriorityName);
156                }
157            }
158        }
159        
160        return result;
161    }
162    
163}