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.PartyEditFactory; 020import com.echothree.control.user.party.common.edit.TimeZoneDescriptionEdit; 021import com.echothree.control.user.party.common.form.EditTimeZoneDescriptionForm; 022import com.echothree.control.user.party.common.result.EditTimeZoneDescriptionResult; 023import com.echothree.control.user.party.common.result.PartyResultFactory; 024import com.echothree.control.user.party.common.spec.TimeZoneDescriptionSpec; 025import com.echothree.model.control.party.server.control.PartyControl; 026import com.echothree.model.data.party.server.entity.Language; 027import com.echothree.model.data.party.server.entity.TimeZone; 028import com.echothree.model.data.party.server.entity.TimeZoneDescription; 029import com.echothree.model.data.party.server.value.TimeZoneDescriptionValue; 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.BaseResult; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.server.control.BaseEditCommand; 037import com.echothree.util.server.persistence.Session; 038import java.util.ArrayList; 039import java.util.Collections; 040import java.util.List; 041 042public class EditTimeZoneDescriptionCommand 043 extends BaseEditCommand<TimeZoneDescriptionSpec, TimeZoneDescriptionEdit> { 044 045 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 046 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 047 048 static { 049 List<FieldDefinition> temp = new ArrayList<>(2); 050 temp.add(new FieldDefinition("JavaTimeZoneName", FieldType.TIME_ZONE_NAME, true, null, null)); 051 temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)); 052 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 053 054 temp = new ArrayList<>(1); 055 temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)); 056 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 057 } 058 059 /** Creates a new instance of EditTimeZoneDescriptionCommand */ 060 public EditTimeZoneDescriptionCommand(UserVisitPK userVisitPK, EditTimeZoneDescriptionForm form) { 061 super(userVisitPK, form, null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 062 } 063 064 @Override 065 protected BaseResult execute() { 066 var partyControl = Session.getModelController(PartyControl.class); 067 EditTimeZoneDescriptionResult result = PartyResultFactory.getEditTimeZoneDescriptionResult(); 068 String javaTimeZoneName = spec.getJavaTimeZoneName(); 069 TimeZone timeZone = partyControl.getTimeZoneByJavaName(javaTimeZoneName); 070 071 if(timeZone != null) { 072 String languageIsoName = spec.getLanguageIsoName(); 073 Language language = partyControl.getLanguageByIsoName(languageIsoName); 074 075 if(language != null) { 076 if(editMode.equals(EditMode.LOCK)) { 077 TimeZoneDescription timeZoneDescription = partyControl.getTimeZoneDescription(timeZone, language); 078 079 if(timeZoneDescription != null) { 080 result.setTimeZoneDescription(partyControl.getTimeZoneDescriptionTransfer(getUserVisit(), timeZoneDescription)); 081 082 if(lockEntity(timeZone)) { 083 TimeZoneDescriptionEdit edit = PartyEditFactory.getTimeZoneDescriptionEdit(); 084 085 result.setEdit(edit); 086 edit.setDescription(timeZoneDescription.getDescription()); 087 } else { 088 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 089 } 090 091 result.setEntityLock(getEntityLockTransfer(timeZone)); 092 } else { 093 addExecutionError(ExecutionErrors.UnknownTimeZoneDescription.name()); 094 } 095 } else if(editMode.equals(EditMode.UPDATE)) { 096 TimeZoneDescriptionValue timeZoneDescriptionValue = partyControl.getTimeZoneDescriptionValueForUpdate(timeZone, language); 097 098 if(timeZoneDescriptionValue != null) { 099 if(lockEntityForUpdate(timeZone)) { 100 try { 101 String description = edit.getDescription(); 102 103 timeZoneDescriptionValue.setDescription(description); 104 105 partyControl.updateTimeZoneDescriptionFromValue(timeZoneDescriptionValue, getPartyPK()); 106 } finally { 107 unlockEntity(timeZone); 108 } 109 } else { 110 addExecutionError(ExecutionErrors.EntityLockStale.name()); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.UnknownTimeZoneDescription.name()); 114 } 115 } 116 } else { 117 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 118 } 119 } else { 120 addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), javaTimeZoneName); 121 } 122 123 return result; 124 } 125 126}