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.party.server.command; 018 019import com.echothree.control.user.party.common.edit.BirthdayFormatEdit; 020import com.echothree.control.user.party.common.edit.PartyEditFactory; 021import com.echothree.control.user.party.common.form.EditBirthdayFormatForm; 022import com.echothree.control.user.party.common.result.EditBirthdayFormatResult; 023import com.echothree.control.user.party.common.result.PartyResultFactory; 024import com.echothree.control.user.party.common.spec.BirthdayFormatSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.party.server.entity.BirthdayFormat; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.EditMode; 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 EditBirthdayFormatCommand 045 extends BaseAbstractEditCommand<BirthdayFormatSpec, BirthdayFormatEdit, EditBirthdayFormatResult, BirthdayFormat, BirthdayFormat> { 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.BirthdayFormat.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("BirthdayFormatName", FieldType.ENTITY_NAME, true, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("BirthdayFormatName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 067 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 068 ); 069 } 070 071 @Inject 072 PartyControl partyControl; 073 074 075 /** Creates a new instance of EditBirthdayFormatCommand */ 076 public EditBirthdayFormatCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditBirthdayFormatResult getResult() { 082 return PartyResultFactory.getEditBirthdayFormatResult(); 083 } 084 085 @Override 086 public BirthdayFormatEdit getEdit() { 087 return PartyEditFactory.getBirthdayFormatEdit(); 088 } 089 090 @Override 091 public BirthdayFormat getEntity(EditBirthdayFormatResult result) { 092 BirthdayFormat birthdayFormat; 093 var birthdayFormatName = spec.getBirthdayFormatName(); 094 095 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 096 birthdayFormat = partyControl.getBirthdayFormatByName(birthdayFormatName); 097 } else { // EditMode.UPDATE 098 birthdayFormat = partyControl.getBirthdayFormatByNameForUpdate(birthdayFormatName); 099 } 100 101 if(birthdayFormat != null) { 102 result.setBirthdayFormat(partyControl.getBirthdayFormatTransfer(getUserVisit(), birthdayFormat)); 103 } else { 104 addExecutionError(ExecutionErrors.UnknownBirthdayFormatName.name(), birthdayFormatName); 105 } 106 107 return birthdayFormat; 108 } 109 110 @Override 111 public BirthdayFormat getLockEntity(BirthdayFormat birthdayFormat) { 112 return birthdayFormat; 113 } 114 115 @Override 116 public void fillInResult(EditBirthdayFormatResult result, BirthdayFormat birthdayFormat) { 117 result.setBirthdayFormat(partyControl.getBirthdayFormatTransfer(getUserVisit(), birthdayFormat)); 118 } 119 120 @Override 121 public void doLock(BirthdayFormatEdit edit, BirthdayFormat birthdayFormat) { 122 var birthdayFormatDescription = partyControl.getBirthdayFormatDescription(birthdayFormat, getPreferredLanguage()); 123 var birthdayFormatDetail = birthdayFormat.getLastDetail(); 124 125 edit.setBirthdayFormatName(birthdayFormatDetail.getBirthdayFormatName()); 126 edit.setIsDefault(birthdayFormatDetail.getIsDefault().toString()); 127 edit.setSortOrder(birthdayFormatDetail.getSortOrder().toString()); 128 129 if(birthdayFormatDescription != null) { 130 edit.setDescription(birthdayFormatDescription.getDescription()); 131 } 132 } 133 134 @Override 135 public void canUpdate(BirthdayFormat birthdayFormat) { 136 var birthdayFormatName = edit.getBirthdayFormatName(); 137 var duplicateBirthdayFormat = partyControl.getBirthdayFormatByName(birthdayFormatName); 138 139 if(duplicateBirthdayFormat != null && !birthdayFormat.equals(duplicateBirthdayFormat)) { 140 addExecutionError(ExecutionErrors.DuplicateBirthdayFormatName.name(), birthdayFormatName); 141 } 142 } 143 144 @Override 145 public void doUpdate(BirthdayFormat birthdayFormat) { 146 var partyPK = getPartyPK(); 147 var birthdayFormatDetailValue = partyControl.getBirthdayFormatDetailValueForUpdate(birthdayFormat); 148 var birthdayFormatDescription = partyControl.getBirthdayFormatDescriptionForUpdate(birthdayFormat, getPreferredLanguage()); 149 var description = edit.getDescription(); 150 151 birthdayFormatDetailValue.setBirthdayFormatName(edit.getBirthdayFormatName()); 152 birthdayFormatDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 153 birthdayFormatDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 154 155 partyControl.updateBirthdayFormatFromValue(birthdayFormatDetailValue, partyPK); 156 157 if(birthdayFormatDescription == null && description != null) { 158 partyControl.createBirthdayFormatDescription(birthdayFormat, getPreferredLanguage(), description, partyPK); 159 } else if(birthdayFormatDescription != null && description == null) { 160 partyControl.deleteBirthdayFormatDescription(birthdayFormatDescription, partyPK); 161 } else if(birthdayFormatDescription != null && description != null) { 162 var birthdayFormatDescriptionValue = partyControl.getBirthdayFormatDescriptionValue(birthdayFormatDescription); 163 164 birthdayFormatDescriptionValue.setDescription(description); 165 partyControl.updateBirthdayFormatDescriptionFromValue(birthdayFormatDescriptionValue, partyPK); 166 } 167 } 168 169}