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.club.server.command; 018 019import com.echothree.control.user.club.common.edit.ClubDescriptionEdit; 020import com.echothree.control.user.club.common.edit.ClubEditFactory; 021import com.echothree.control.user.club.common.form.EditClubDescriptionForm; 022import com.echothree.control.user.club.common.result.ClubResultFactory; 023import com.echothree.control.user.club.common.result.EditClubDescriptionResult; 024import com.echothree.control.user.club.common.spec.ClubDescriptionSpec; 025import com.echothree.model.control.club.server.control.ClubControl; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.data.club.server.entity.Club; 028import com.echothree.model.data.club.server.entity.ClubDescription; 029import com.echothree.model.data.club.server.value.ClubDescriptionValue; 030import com.echothree.model.data.party.server.entity.Language; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.BaseResult; 036import com.echothree.util.common.command.EditMode; 037import com.echothree.util.server.control.BaseEditCommand; 038import com.echothree.util.server.persistence.Session; 039import java.util.Arrays; 040import java.util.Collections; 041import java.util.List; 042 043public class EditClubDescriptionCommand 044 extends BaseEditCommand<ClubDescriptionSpec, ClubDescriptionEdit> { 045 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 051 new FieldDefinition("ClubName", FieldType.ENTITY_NAME, true, null, null), 052 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 053 )); 054 055 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 056 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 057 )); 058 } 059 060 /** Creates a new instance of EditClubDescriptionCommand */ 061 public EditClubDescriptionCommand(UserVisitPK userVisitPK, EditClubDescriptionForm form) { 062 super(userVisitPK, form, null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 063 } 064 065 @Override 066 protected BaseResult execute() { 067 var clubControl = Session.getModelController(ClubControl.class); 068 EditClubDescriptionResult result = ClubResultFactory.getEditClubDescriptionResult(); 069 String clubName = spec.getClubName(); 070 Club club = clubControl.getClubByName(clubName); 071 072 if(club != null) { 073 var partyControl = Session.getModelController(PartyControl.class); 074 String languageIsoName = spec.getLanguageIsoName(); 075 Language language = partyControl.getLanguageByIsoName(languageIsoName); 076 077 if(language != null) { 078 if(editMode.equals(EditMode.LOCK)) { 079 ClubDescription clubDescription = clubControl.getClubDescription(club, language); 080 081 if(clubDescription != null) { 082 result.setClubDescription(clubControl.getClubDescriptionTransfer(getUserVisit(), clubDescription)); 083 084 if(lockEntity(club)) { 085 ClubDescriptionEdit edit = ClubEditFactory.getClubDescriptionEdit(); 086 087 result.setEdit(edit); 088 edit.setDescription(clubDescription.getDescription()); 089 } else { 090 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 091 } 092 093 result.setEntityLock(getEntityLockTransfer(club)); 094 } else { 095 addExecutionError(ExecutionErrors.UnknownClubDescription.name()); 096 } 097 } else if(editMode.equals(EditMode.UPDATE)) { 098 ClubDescriptionValue clubDescriptionValue = clubControl.getClubDescriptionValueForUpdate(club, language); 099 100 if(clubDescriptionValue != null) { 101 if(lockEntityForUpdate(club)) { 102 try { 103 String description = edit.getDescription(); 104 105 clubDescriptionValue.setDescription(description); 106 107 clubControl.updateClubDescriptionFromValue(clubDescriptionValue, getPartyPK()); 108 } finally { 109 unlockEntity(club); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.EntityLockStale.name()); 113 } 114 } else { 115 addExecutionError(ExecutionErrors.UnknownClubDescription.name()); 116 } 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownClubName.name(), clubName); 123 } 124 125 return result; 126 } 127 128}