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.form.CreateContentPageAreaForm; 020import com.echothree.model.control.content.server.control.ContentControl; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.control.security.common.SecurityRoleGroups; 024import com.echothree.model.control.security.common.SecurityRoles; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.server.control.BaseSimpleCommand; 031import com.echothree.util.server.control.CommandSecurityDefinition; 032import com.echothree.util.server.control.PartyTypeDefinition; 033import com.echothree.util.server.control.SecurityRoleDefinition; 034import java.util.List; 035import javax.enterprise.context.Dependent; 036import javax.inject.Inject; 037 038@Dependent 039public class CreateContentPageAreaCommand 040 extends BaseSimpleCommand<CreateContentPageAreaForm> { 041 042 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 047 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 048 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 049 new SecurityRoleDefinition(SecurityRoleGroups.ContentPageArea.name(), SecurityRoles.Create.name()) 050 )) 051 )); 052 053 FORM_FIELD_DEFINITIONS = List.of( 054 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 055 new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null), 056 new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null), 057 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 058 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null), 060 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L), 061 // ContentPageAreaBlob is not validated 062 new FieldDefinition("ContentPageAreaClob", FieldType.STRING, false, 1L, null), 063 new FieldDefinition("ContentPageAreaUrl", FieldType.URL, false, 1L, 200L) 064 ); 065 } 066 067 @Inject 068 ContentControl contentControl; 069 070 @Inject 071 PartyControl partyControl; 072 073 074 /** Creates a new instance of CreateContentPageAreaCommand */ 075 public CreateContentPageAreaCommand() { 076 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 077 } 078 079 @Override 080 protected BaseResult execute() { 081 var contentCollectionName = form.getContentCollectionName(); 082 var contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 083 084 if(contentCollection != null) { 085 var contentSectionName = form.getContentSectionName(); 086 var contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName); 087 088 if(contentSection != null) { 089 var contentPageName = form.getContentPageName(); 090 var contentPage = contentControl.getContentPageByName(contentSection, contentPageName); 091 092 if(contentPage != null) { 093 var sortOrder = Integer.valueOf(form.getSortOrder()); 094 var contentPageLayout = contentPage.getLastDetail().getContentPageLayout(); 095 var contentPageLayoutArea = contentControl.getContentPageLayoutArea(contentPageLayout, sortOrder); 096 097 if(contentPageLayoutArea != null) { 098 var languageIsoName = form.getLanguageIsoName(); 099 var language = partyControl.getLanguageByIsoName(languageIsoName); 100 101 if(language != null) { 102 var contentPageArea = contentControl.getContentPageArea(contentPage, contentPageLayoutArea, language); 103 104 if(contentPageArea == null) { 105 var mimeTypeName = form.getMimeTypeName(); 106 var mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName); 107 108 if(mimeType != null) { 109 var description = form.getDescription(); 110 var contentPageAreaBlob = form.getContentPageAreaBlob(); 111 var contentPageAreaClob = form.getContentPageAreaClob(); 112 var contentPageAreaUrl = form.getContentPageAreaUrl(); 113 114 contentPageArea = contentControl.createContentPageArea(contentPage, contentPageLayoutArea, language, mimeType, getPartyPK()); 115 var contentPageAreaDetail = contentPageArea.getLastDetail(); 116 117 if(contentPageAreaBlob != null) 118 contentControl.createContentPageAreaBlob(contentPageAreaDetail, contentPageAreaBlob); 119 120 if(contentPageAreaClob != null) 121 contentControl.createContentPageAreaClob(contentPageAreaDetail, contentPageAreaClob); 122 123 if(contentPageAreaUrl != null) 124 contentControl.createContentPageAreaUrl(contentPageAreaDetail, contentPageAreaUrl); 125 126 if(description != null) 127 contentControl.createContentPageAreaString(contentPageAreaDetail, description); 128 } else { 129 addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.DuplicateContentPageArea.name()); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.UnknownContentPageLayoutArea.name()); 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName); 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 148 } 149 150 return null; 151 } 152 153}