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