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.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.party.server.entity.BirthdayFormatDescription; 031import com.echothree.model.data.party.server.entity.BirthdayFormatDetail; 032import com.echothree.model.data.party.server.value.BirthdayFormatDescriptionValue; 033import com.echothree.model.data.party.server.value.BirthdayFormatDetailValue; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.common.command.EditMode; 039import com.echothree.util.server.control.BaseAbstractEditCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import com.echothree.util.server.persistence.Session; 044import java.util.Arrays; 045import java.util.Collections; 046import java.util.List; 047 048public class EditBirthdayFormatCommand 049 extends BaseAbstractEditCommand<BirthdayFormatSpec, BirthdayFormatEdit, EditBirthdayFormatResult, BirthdayFormat, BirthdayFormat> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 059 new SecurityRoleDefinition(SecurityRoleGroups.BirthdayFormat.name(), SecurityRoles.Edit.name()) 060 ))) 061 ))); 062 063 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 064 new FieldDefinition("BirthdayFormatName", FieldType.ENTITY_NAME, true, null, null) 065 )); 066 067 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("BirthdayFormatName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 070 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 071 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 072 )); 073 } 074 075 /** Creates a new instance of EditBirthdayFormatCommand */ 076 public EditBirthdayFormatCommand(UserVisitPK userVisitPK, EditBirthdayFormatForm form) { 077 super(userVisitPK, form, 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 var partyControl = Session.getModelController(PartyControl.class); 093 BirthdayFormat birthdayFormat = null; 094 String birthdayFormatName = spec.getBirthdayFormatName(); 095 096 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 097 birthdayFormat = partyControl.getBirthdayFormatByName(birthdayFormatName); 098 } else { // EditMode.UPDATE 099 birthdayFormat = partyControl.getBirthdayFormatByNameForUpdate(birthdayFormatName); 100 } 101 102 if(birthdayFormat != null) { 103 result.setBirthdayFormat(partyControl.getBirthdayFormatTransfer(getUserVisit(), birthdayFormat)); 104 } else { 105 addExecutionError(ExecutionErrors.UnknownBirthdayFormatName.name(), birthdayFormatName); 106 } 107 108 return birthdayFormat; 109 } 110 111 @Override 112 public BirthdayFormat getLockEntity(BirthdayFormat birthdayFormat) { 113 return birthdayFormat; 114 } 115 116 @Override 117 public void fillInResult(EditBirthdayFormatResult result, BirthdayFormat birthdayFormat) { 118 var partyControl = Session.getModelController(PartyControl.class); 119 120 result.setBirthdayFormat(partyControl.getBirthdayFormatTransfer(getUserVisit(), birthdayFormat)); 121 } 122 123 @Override 124 public void doLock(BirthdayFormatEdit edit, BirthdayFormat birthdayFormat) { 125 var partyControl = Session.getModelController(PartyControl.class); 126 BirthdayFormatDescription birthdayFormatDescription = partyControl.getBirthdayFormatDescription(birthdayFormat, getPreferredLanguage()); 127 BirthdayFormatDetail birthdayFormatDetail = birthdayFormat.getLastDetail(); 128 129 edit.setBirthdayFormatName(birthdayFormatDetail.getBirthdayFormatName()); 130 edit.setIsDefault(birthdayFormatDetail.getIsDefault().toString()); 131 edit.setSortOrder(birthdayFormatDetail.getSortOrder().toString()); 132 133 if(birthdayFormatDescription != null) { 134 edit.setDescription(birthdayFormatDescription.getDescription()); 135 } 136 } 137 138 @Override 139 public void canUpdate(BirthdayFormat birthdayFormat) { 140 var partyControl = Session.getModelController(PartyControl.class); 141 String birthdayFormatName = edit.getBirthdayFormatName(); 142 BirthdayFormat duplicateBirthdayFormat = partyControl.getBirthdayFormatByName(birthdayFormatName); 143 144 if(duplicateBirthdayFormat != null && !birthdayFormat.equals(duplicateBirthdayFormat)) { 145 addExecutionError(ExecutionErrors.DuplicateBirthdayFormatName.name(), birthdayFormatName); 146 } 147 } 148 149 @Override 150 public void doUpdate(BirthdayFormat birthdayFormat) { 151 var partyControl = Session.getModelController(PartyControl.class); 152 var partyPK = getPartyPK(); 153 BirthdayFormatDetailValue birthdayFormatDetailValue = partyControl.getBirthdayFormatDetailValueForUpdate(birthdayFormat); 154 BirthdayFormatDescription birthdayFormatDescription = partyControl.getBirthdayFormatDescriptionForUpdate(birthdayFormat, getPreferredLanguage()); 155 String description = edit.getDescription(); 156 157 birthdayFormatDetailValue.setBirthdayFormatName(edit.getBirthdayFormatName()); 158 birthdayFormatDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 159 birthdayFormatDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 160 161 partyControl.updateBirthdayFormatFromValue(birthdayFormatDetailValue, partyPK); 162 163 if(birthdayFormatDescription == null && description != null) { 164 partyControl.createBirthdayFormatDescription(birthdayFormat, getPreferredLanguage(), description, partyPK); 165 } else if(birthdayFormatDescription != null && description == null) { 166 partyControl.deleteBirthdayFormatDescription(birthdayFormatDescription, partyPK); 167 } else if(birthdayFormatDescription != null && description != null) { 168 BirthdayFormatDescriptionValue birthdayFormatDescriptionValue = partyControl.getBirthdayFormatDescriptionValue(birthdayFormatDescription); 169 170 birthdayFormatDescriptionValue.setDescription(description); 171 partyControl.updateBirthdayFormatDescriptionFromValue(birthdayFormatDescriptionValue, partyPK); 172 } 173 } 174 175}