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.GetContentPageAreasForm; 020import com.echothree.control.user.content.common.result.ContentResultFactory; 021import com.echothree.model.control.content.server.control.ContentControl; 022import com.echothree.model.data.content.server.entity.ContentPage; 023import com.echothree.model.data.content.server.entity.ContentPageArea; 024import com.echothree.model.data.content.server.factory.ContentPageLayoutAreaFactory; 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.BasePaginatedMultipleEntitiesCommand; 031import java.util.Collection; 032import java.util.List; 033import javax.enterprise.context.Dependent; 034import javax.inject.Inject; 035 036@Dependent 037public class GetContentPageAreasCommand 038 extends BasePaginatedMultipleEntitiesCommand<ContentPageArea, GetContentPageAreasForm> { 039 040 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 041 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 042 043 static { 044 FORM_FIELD_DEFINITIONS = List.of( 045 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 046 new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null) 048 ); 049 } 050 051 @Inject 052 ContentControl contentControl; 053 054 055 /** Creates a new instance of GetContentPageAreasCommand */ 056 public GetContentPageAreasCommand() { 057 super(null, FORM_FIELD_DEFINITIONS, true); 058 } 059 060 ContentPage contentPage; 061 062 @Override 063 protected void handleForm() { 064 var contentCollectionName = form.getContentCollectionName(); 065 var contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 066 067 if(contentCollection != null) { 068 var contentSectionName = form.getContentSectionName(); 069 var contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName); 070 071 if(contentSection != null) { 072 var contentPageName = form.getContentPageName(); 073 074 contentPage = contentControl.getContentPageByName(contentSection, contentPageName); 075 076 if(contentPage == null) { 077 addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName); 078 } 079 } else { 080 addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName); 081 } 082 } else { 083 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 084 } 085 } 086 087 @Override 088 protected Long getTotalEntities() { 089 return hasExecutionErrors() ? null : 090 contentControl.countContentPageAreasByContentPage(contentPage); 091 } 092 093 @Override 094 protected Collection<ContentPageArea> getEntities() { 095 Collection<ContentPageArea> contentPageAreas = null; 096 097 if(!hasExecutionErrors()) { 098 contentPageAreas = contentControl.getContentPageAreasByContentPage(contentPage); 099 } 100 101 return contentPageAreas; 102 } 103 104 @Override 105 protected BaseResult getResult(Collection<ContentPageArea> entities) { 106 var result = ContentResultFactory.getGetContentPageAreasResult(); 107 108 if(entities != null) { 109 var userVisit = getUserVisit(); 110 111 result.setContentPage(contentControl.getContentPageTransfer(userVisit, contentPage)); 112 113 if(session.hasLimit(ContentPageLayoutAreaFactory.class)) { 114 result.setContentPageAreaCount(getTotalEntities()); 115 } 116 117 result.setContentPageAreas(contentControl.getContentPageAreaTransfers(getUserVisit(), entities)); 118 } 119 120 return result; 121 } 122 123}