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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.FontWeightDescriptionEdit; 021import com.echothree.control.user.core.common.form.EditFontWeightDescriptionForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditFontWeightDescriptionResult; 024import com.echothree.control.user.core.common.spec.FontWeightDescriptionSpec; 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.core.server.entity.FontWeight; 030import com.echothree.model.data.core.server.entity.FontWeightDescription; 031import com.echothree.model.data.core.server.value.FontWeightDescriptionValue; 032import com.echothree.model.data.party.server.entity.Language; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.common.command.EditMode; 038import com.echothree.util.server.control.BaseAbstractEditCommand; 039import com.echothree.util.server.control.CommandSecurityDefinition; 040import com.echothree.util.server.control.PartyTypeDefinition; 041import com.echothree.util.server.control.SecurityRoleDefinition; 042import com.echothree.util.server.persistence.Session; 043import java.util.Arrays; 044import java.util.Collections; 045import java.util.List; 046 047public class EditFontWeightDescriptionCommand 048 extends BaseAbstractEditCommand<FontWeightDescriptionSpec, FontWeightDescriptionEdit, EditFontWeightDescriptionResult, FontWeightDescription, FontWeight> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 052 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 053 054 static { 055 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 058 new SecurityRoleDefinition(SecurityRoleGroups.FontWeight.name(), SecurityRoles.Description.name()) 059 ))) 060 ))); 061 062 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("FontWeightName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 065 )); 066 067 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 069 )); 070 } 071 072 /** Creates a new instance of EditFontWeightDescriptionCommand */ 073 public EditFontWeightDescriptionCommand(UserVisitPK userVisitPK, EditFontWeightDescriptionForm form) { 074 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 075 } 076 077 @Override 078 public EditFontWeightDescriptionResult getResult() { 079 return CoreResultFactory.getEditFontWeightDescriptionResult(); 080 } 081 082 @Override 083 public FontWeightDescriptionEdit getEdit() { 084 return CoreEditFactory.getFontWeightDescriptionEdit(); 085 } 086 087 @Override 088 public FontWeightDescription getEntity(EditFontWeightDescriptionResult result) { 089 var coreControl = getCoreControl(); 090 FontWeightDescription fontWeightDescription = null; 091 String fontWeightName = spec.getFontWeightName(); 092 FontWeight fontWeight = coreControl.getFontWeightByName(fontWeightName); 093 094 if(fontWeight != null) { 095 var partyControl = Session.getModelController(PartyControl.class); 096 String languageIsoName = spec.getLanguageIsoName(); 097 Language language = partyControl.getLanguageByIsoName(languageIsoName); 098 099 if(language != null) { 100 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 101 fontWeightDescription = coreControl.getFontWeightDescription(fontWeight, language); 102 } else { // EditMode.UPDATE 103 fontWeightDescription = coreControl.getFontWeightDescriptionForUpdate(fontWeight, language); 104 } 105 106 if(fontWeightDescription == null) { 107 addExecutionError(ExecutionErrors.UnknownFontWeightDescription.name(), fontWeightName, languageIsoName); 108 } 109 } else { 110 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.UnknownFontWeightName.name(), fontWeightName); 114 } 115 116 return fontWeightDescription; 117 } 118 119 @Override 120 public FontWeight getLockEntity(FontWeightDescription fontWeightDescription) { 121 return fontWeightDescription.getFontWeight(); 122 } 123 124 @Override 125 public void fillInResult(EditFontWeightDescriptionResult result, FontWeightDescription fontWeightDescription) { 126 var coreControl = getCoreControl(); 127 128 result.setFontWeightDescription(coreControl.getFontWeightDescriptionTransfer(getUserVisit(), fontWeightDescription)); 129 } 130 131 @Override 132 public void doLock(FontWeightDescriptionEdit edit, FontWeightDescription fontWeightDescription) { 133 edit.setDescription(fontWeightDescription.getDescription()); 134 } 135 136 @Override 137 public void doUpdate(FontWeightDescription fontWeightDescription) { 138 var coreControl = getCoreControl(); 139 FontWeightDescriptionValue fontWeightDescriptionValue = coreControl.getFontWeightDescriptionValue(fontWeightDescription); 140 fontWeightDescriptionValue.setDescription(edit.getDescription()); 141 142 coreControl.updateFontWeightDescriptionFromValue(fontWeightDescriptionValue, getPartyPK()); 143 } 144 145}