001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.ContentSectionEdit; 021import com.echothree.control.user.content.common.form.EditContentSectionForm; 022import com.echothree.control.user.content.common.result.ContentResultFactory; 023import com.echothree.control.user.content.common.result.EditContentSectionResult; 024import com.echothree.control.user.content.common.spec.ContentSectionSpec; 025import com.echothree.model.control.content.common.ContentSections; 026import com.echothree.model.control.content.server.control.ContentControl; 027import com.echothree.model.control.party.common.PartyTypes; 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.user.common.pk.UserVisitPK; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.common.command.EditMode; 037import com.echothree.util.server.control.BaseAbstractEditCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 046public class EditContentSectionCommand 047 extends BaseAbstractEditCommand<ContentSectionSpec, ContentSectionEdit, EditContentSectionResult, ContentSection, ContentSection> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 051 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.ContentSection.name(), SecurityRoles.Edit.name()) 058 )) 059 )); 060 061 SPEC_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null) 064 ); 065 066 EDIT_FIELD_DEFINITIONS = List.of( 067 new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("ParentContentSectionName", FieldType.ENTITY_NAME, false, 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 @Inject 076 ContentControl contentControl; 077 078 079 /** Creates a new instance of EditContentSectionCommand */ 080 public EditContentSectionCommand() { 081 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 082 } 083 084 @Override 085 public EditContentSectionResult getResult() { 086 return ContentResultFactory.getEditContentSectionResult(); 087 } 088 089 @Override 090 public ContentSectionEdit getEdit() { 091 return ContentEditFactory.getContentSectionEdit(); 092 } 093 094 ContentCollection contentCollection = null; 095 096 @Override 097 public ContentSection getEntity(EditContentSectionResult result) { 098 ContentSection contentSection = null; 099 var contentCollectionName = spec.getContentCollectionName(); 100 101 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 102 103 if(contentCollection != null) { 104 var contentSectionName = spec.getContentSectionName(); 105 106 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 107 contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName); 108 } else { // EditMode.UPDATE 109 contentSection = contentControl.getContentSectionByNameForUpdate(contentCollection, contentSectionName); 110 } 111 112 if(contentSection != null) { 113 result.setContentSection(contentControl.getContentSectionTransfer(getUserVisit(), contentSection)); 114 } else { 115 addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 119 } 120 121 return contentSection; 122 } 123 124 @Override 125 public ContentSection getLockEntity(ContentSection contentSection) { 126 return contentSection; 127 } 128 129 @Override 130 public void fillInResult(EditContentSectionResult result, ContentSection contentSection) { 131 result.setContentSection(contentControl.getContentSectionTransfer(getUserVisit(), contentSection)); 132 } 133 134 @Override 135 public void doLock(ContentSectionEdit edit, ContentSection contentSection) { 136 var contentSectionDescription = contentControl.getContentSectionDescription(contentSection, getPreferredLanguage()); 137 var contentSectionDetail = contentSection.getLastDetail(); 138 var parentContentSection = contentSectionDetail.getParentContentSection(); 139 140 edit.setContentSectionName(contentSectionDetail.getContentSectionName()); 141 edit.setParentContentSectionName(parentContentSection == null? null: parentContentSection.getLastDetail().getContentSectionName()); 142 edit.setIsDefault(contentSectionDetail.getIsDefault().toString()); 143 edit.setSortOrder(contentSectionDetail.getSortOrder().toString()); 144 145 if(contentSectionDescription != null) { 146 edit.setDescription(contentSectionDescription.getDescription()); 147 } 148 } 149 150 ContentSection parentContentSection = null; 151 152 @Override 153 public void canUpdate(ContentSection contentSection) { 154 var contentSectionName = edit.getContentSectionName(); 155 var duplicateContentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName); 156 157 if(duplicateContentSection == null || contentSection.equals(duplicateContentSection)) { 158 var parentContentSectionName = edit.getParentContentSectionName(); 159 160 parentContentSection = contentControl.getContentSectionByName(contentCollection, parentContentSectionName == null ? 161 ContentSections.ROOT.toString() : parentContentSectionName); 162 163 if(parentContentSection != null) { 164 if(!contentControl.isParentContentSectionSafe(contentSection, parentContentSection)) { 165 addExecutionError(ExecutionErrors.InvalidParentContentSection.name()); 166 } 167 } else { 168 addExecutionError(ExecutionErrors.UnknownParentContentSectionName.name(), parentContentSectionName); 169 } 170 } else { 171 addExecutionError(ExecutionErrors.DuplicateContentSectionName.name(), contentSectionName); 172 } 173 } 174 175 @Override 176 public void doUpdate(ContentSection contentSection) { 177 var partyPK = getPartyPK(); 178 var contentSectionDetailValue = contentControl.getContentSectionDetailValueForUpdate(contentSection); 179 var contentSectionDescription = contentControl.getContentSectionDescriptionForUpdate(contentSection, getPreferredLanguage()); 180 var description = edit.getDescription(); 181 182 contentSectionDetailValue.setContentSectionName(edit.getContentSectionName()); 183 contentSectionDetailValue.setParentContentSectionPK(parentContentSection == null? null: parentContentSection.getPrimaryKey()); 184 contentSectionDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 185 contentSectionDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 186 187 contentControl.updateContentSectionFromValue(contentSectionDetailValue, partyPK); 188 189 if(contentSectionDescription == null && description != null) { 190 contentControl.createContentSectionDescription(contentSection, getPreferredLanguage(), description, partyPK); 191 } else if(contentSectionDescription != null && description == null) { 192 contentControl.deleteContentSectionDescription(contentSectionDescription, partyPK); 193 } else if(contentSectionDescription != null && description != null) { 194 var contentSectionDescriptionValue = contentControl.getContentSectionDescriptionValue(contentSectionDescription); 195 196 contentSectionDescriptionValue.setDescription(description); 197 contentControl.updateContentSectionDescriptionFromValue(contentSectionDescriptionValue, partyPK); 198 } 199 } 200 201}