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.GetContentCategoriesForm; 020import com.echothree.control.user.content.common.result.ContentResultFactory; 021import com.echothree.model.control.associate.server.logic.AssociateReferralLogic; 022import com.echothree.model.control.content.server.control.ContentControl; 023import com.echothree.model.data.content.server.entity.ContentCatalog; 024import com.echothree.model.data.content.server.entity.ContentCategory; 025import com.echothree.model.data.content.server.entity.ContentCollection; 026import com.echothree.model.data.content.server.factory.ContentCategoryFactory; 027import com.echothree.model.data.user.common.pk.UserVisitPK; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 033import com.echothree.util.server.persistence.Session; 034import java.util.Collection; 035import java.util.List; 036import javax.enterprise.context.Dependent; 037 038@Dependent 039public class GetContentCategoriesCommand 040 extends BasePaginatedMultipleEntitiesCommand<ContentCategory, GetContentCategoriesForm> { 041 042 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = List.of( 047 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null), 048 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null), 049 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("ParentContentCategoryName", FieldType.ENTITY_NAME, false, null, null), 051 new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null), 052 new FieldDefinition("AssociateName", FieldType.STRING, false, null, null), 053 new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null) 054 ); 055 } 056 057 /** Creates a new instance of GetContentCategoriesCommand */ 058 public GetContentCategoriesCommand() { 059 super(null, FORM_FIELD_DEFINITIONS, true); 060 } 061 062 private ContentCatalog contentCatalog; 063 private ContentCategory parentContentCategory; 064 065 @Override 066 protected void handleForm() { 067 var contentWebAddressName = form.getContentWebAddressName(); 068 var contentCollectionName = form.getContentCollectionName(); 069 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 070 071 if(parameterCount == 1) { 072 var contentControl = Session.getModelController(ContentControl.class); 073 ContentCollection contentCollection = null; 074 075 if(contentWebAddressName != null) { 076 var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 077 078 if(contentWebAddress != null) { 079 contentCollection = contentWebAddress.getLastDetail().getContentCollection(); 080 } else { 081 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 082 } 083 } else { 084 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 085 086 if(contentCollection == null) { 087 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 088 } 089 } 090 091 if(!hasExecutionErrors()) { 092 var contentCatalogName = form.getContentCatalogName(); 093 094 contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 095 096 if(contentCatalogName == null || contentCatalog != null) { 097 if(contentCatalog == null) { 098 contentCatalog = contentControl.getDefaultContentCatalog(contentCollection); 099 } 100 101 if(contentCatalog != null) { 102 var parentContentCategoryName = form.getParentContentCategoryName(); 103 104 if(parentContentCategoryName == null) { 105 parentContentCategory = null; 106 } else { 107 parentContentCategory = contentControl.getContentCategoryByName(contentCatalog, parentContentCategoryName); 108 109 if(parentContentCategory == null) { 110 addExecutionError(ExecutionErrors.UnknownParentContentCategoryName.name(), 111 contentCollection.getLastDetail().getContentCollectionName(), parentContentCategoryName); 112 } 113 } 114 115 } else { 116 addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), 117 contentCollection.getLastDetail().getContentCollectionName()); 118 } 119 } else { 120 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), 121 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName); 122 } 123 } 124 } else { 125 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 126 } 127 128 if(!hasExecutionErrors()) { 129 AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(), 130 contentCatalog.getPrimaryKey(), getPartyPK()); 131 } 132 } 133 134 @Override 135 protected Long getTotalEntities() { 136 var contentControl = Session.getModelController(ContentControl.class); 137 138 return hasExecutionErrors() ? null : 139 parentContentCategory == null ? 140 contentControl.countContentCategoriesByContentCatalog(contentCatalog) : 141 contentControl.countContentCategoriesByParentContentCategory(parentContentCategory); 142 } 143 144 145 @Override 146 protected Collection<ContentCategory> getEntities() { 147 Collection<ContentCategory> contentCategories = null; 148 149 if(!hasExecutionErrors()) { 150 var contentControl = Session.getModelController(ContentControl.class); 151 152 if(parentContentCategory == null) { 153 contentCategories = contentControl.getContentCategories(contentCatalog); 154 } else { 155 contentCategories = contentControl.getContentCategoriesByParentContentCategory(parentContentCategory); 156 } 157 } 158 159 return contentCategories; 160 } 161 162 @Override 163 protected BaseResult getResult(Collection<ContentCategory> entities) { 164 var result = ContentResultFactory.getGetContentCategoriesResult(); 165 166 if(entities != null) { 167 var contentControl = Session.getModelController(ContentControl.class); 168 var userVisit = getUserVisit(); 169 170 result.setContentCatalog(contentControl.getContentCatalogTransfer(userVisit, contentCatalog)); 171 172 if(parentContentCategory != null) { 173 result.setParentContentCategory(contentControl.getContentCategoryTransfer(userVisit, parentContentCategory)); 174 } 175 176 if(session.hasLimit(ContentCategoryFactory.class)) { 177 result.setContentCategoryCount(getTotalEntities()); 178 } 179 180 result.setContentCategories(contentControl.getContentCategoryTransfers(userVisit, entities)); 181 } 182 183 return result; 184 } 185 186}