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.content.server.command; 018 019import com.echothree.control.user.content.common.edit.ContentEditFactory; 020import com.echothree.control.user.content.common.edit.ContentSectionDescriptionEdit; 021import com.echothree.control.user.content.common.form.EditContentSectionDescriptionForm; 022import com.echothree.control.user.content.common.result.ContentResultFactory; 023import com.echothree.control.user.content.common.result.EditContentSectionDescriptionResult; 024import com.echothree.control.user.content.common.spec.ContentSectionDescriptionSpec; 025import com.echothree.model.control.content.server.control.ContentControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.content.server.entity.ContentCollection; 031import com.echothree.model.data.content.server.entity.ContentSection; 032import com.echothree.model.data.content.server.entity.ContentSectionDescription; 033import com.echothree.model.data.content.server.value.ContentSectionDescriptionValue; 034import com.echothree.model.data.party.server.entity.Language; 035import com.echothree.model.data.user.common.pk.UserVisitPK; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.common.command.EditMode; 040import com.echothree.util.server.control.BaseAbstractEditCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import com.echothree.util.server.persistence.Session; 045import java.util.Arrays; 046import java.util.Collections; 047import java.util.List; 048 049public class EditContentSectionDescriptionCommand 050 extends BaseAbstractEditCommand<ContentSectionDescriptionSpec, ContentSectionDescriptionEdit, EditContentSectionDescriptionResult, ContentSectionDescription, ContentSection> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 054 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 060 new SecurityRoleDefinition(SecurityRoleGroups.ContentSection.name(), SecurityRoles.Description.name()) 061 ))) 062 ))); 063 064 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 065 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 068 )); 069 070 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 071 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 072 )); 073 } 074 075 /** Creates a new instance of EditContentSectionDescriptionCommand */ 076 public EditContentSectionDescriptionCommand(UserVisitPK userVisitPK, EditContentSectionDescriptionForm form) { 077 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditContentSectionDescriptionResult getResult() { 082 return ContentResultFactory.getEditContentSectionDescriptionResult(); 083 } 084 085 @Override 086 public ContentSectionDescriptionEdit getEdit() { 087 return ContentEditFactory.getContentSectionDescriptionEdit(); 088 } 089 090 @Override 091 public ContentSectionDescription getEntity(EditContentSectionDescriptionResult result) { 092 var contentControl = Session.getModelController(ContentControl.class); 093 ContentSectionDescription contentSectionDescription = null; 094 String contentCollectionName = spec.getContentCollectionName(); 095 ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 096 097 if(contentCollection != null) { 098 String contentSectionName = spec.getContentSectionName(); 099 ContentSection contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName); 100 101 if(contentSection != null) { 102 var partyControl = Session.getModelController(PartyControl.class); 103 String languageIsoName = spec.getLanguageIsoName(); 104 Language language = partyControl.getLanguageByIsoName(languageIsoName); 105 106 result.setContentSection(contentControl.getContentSectionTransfer(getUserVisit(), contentSection)); 107 108 if(language != null) { 109 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 110 contentSectionDescription = contentControl.getContentSectionDescription(contentSection, language); 111 } else { // EditMode.UPDATE 112 contentSectionDescription = contentControl.getContentSectionDescriptionForUpdate(contentSection, language); 113 } 114 115 if(contentSectionDescription == null) { 116 addExecutionError(ExecutionErrors.UnknownContentSectionDescription.name()); 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 126 } 127 128 return contentSectionDescription; 129 } 130 131 @Override 132 public ContentSection getLockEntity(ContentSectionDescription contentSectionDescription) { 133 return contentSectionDescription.getContentSection(); 134 } 135 136 @Override 137 public void fillInResult(EditContentSectionDescriptionResult result, ContentSectionDescription contentSectionDescription) { 138 var contentControl = Session.getModelController(ContentControl.class); 139 140 result.setContentSectionDescription(contentControl.getContentSectionDescriptionTransfer(getUserVisit(), contentSectionDescription)); 141 } 142 143 @Override 144 public void doLock(ContentSectionDescriptionEdit edit, ContentSectionDescription contentSectionDescription) { 145 edit.setDescription(contentSectionDescription.getDescription()); 146 } 147 148 @Override 149 public void doUpdate(ContentSectionDescription contentSectionDescription) { 150 var contentControl = Session.getModelController(ContentControl.class); 151 ContentSectionDescriptionValue contentSectionDescriptionValue = contentControl.getContentSectionDescriptionValue(contentSectionDescription); 152 contentSectionDescriptionValue.setDescription(edit.getDescription()); 153 154 contentControl.updateContentSectionDescriptionFromValue(contentSectionDescriptionValue, getPartyPK()); 155 } 156 157}