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 java.util.Collection; 034import java.util.List; 035import javax.enterprise.context.Dependent; 036import javax.inject.Inject; 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 @Inject 058 ContentControl contentControl; 059 060 @Inject 061 AssociateReferralLogic associateReferralLogic; 062 063 064 /** Creates a new instance of GetContentCategoriesCommand */ 065 public GetContentCategoriesCommand() { 066 super(null, FORM_FIELD_DEFINITIONS, true); 067 } 068 069 private ContentCatalog contentCatalog; 070 private ContentCategory parentContentCategory; 071 072 @Override 073 protected void handleForm() { 074 var contentWebAddressName = form.getContentWebAddressName(); 075 var contentCollectionName = form.getContentCollectionName(); 076 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 077 078 if(parameterCount == 1) { 079 ContentCollection contentCollection = null; 080 081 if(contentWebAddressName != null) { 082 var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 083 084 if(contentWebAddress != null) { 085 contentCollection = contentWebAddress.getLastDetail().getContentCollection(); 086 } else { 087 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 088 } 089 } else { 090 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 091 092 if(contentCollection == null) { 093 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 094 } 095 } 096 097 if(!hasExecutionErrors()) { 098 var contentCatalogName = form.getContentCatalogName(); 099 100 contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 101 102 if(contentCatalogName == null || contentCatalog != null) { 103 if(contentCatalog == null) { 104 contentCatalog = contentControl.getDefaultContentCatalog(contentCollection); 105 } 106 107 if(contentCatalog != null) { 108 var parentContentCategoryName = form.getParentContentCategoryName(); 109 110 if(parentContentCategoryName == null) { 111 parentContentCategory = null; 112 } else { 113 parentContentCategory = contentControl.getContentCategoryByName(contentCatalog, parentContentCategoryName); 114 115 if(parentContentCategory == null) { 116 addExecutionError(ExecutionErrors.UnknownParentContentCategoryName.name(), 117 contentCollection.getLastDetail().getContentCollectionName(), parentContentCategoryName); 118 } 119 } 120 121 } else { 122 addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), 123 contentCollection.getLastDetail().getContentCollectionName()); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), 127 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName); 128 } 129 } 130 } else { 131 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 132 } 133 134 if(!hasExecutionErrors()) { 135 associateReferralLogic.handleAssociateReferral(session, this, form, getUserVisitForUpdate(), 136 contentCatalog.getPrimaryKey(), getPartyPK()); 137 } 138 } 139 140 @Override 141 protected Long getTotalEntities() { 142 return hasExecutionErrors() ? null : 143 parentContentCategory == null ? 144 contentControl.countContentCategoriesByContentCatalog(contentCatalog) : 145 contentControl.countContentCategoriesByParentContentCategory(parentContentCategory); 146 } 147 148 149 @Override 150 protected Collection<ContentCategory> getEntities() { 151 Collection<ContentCategory> contentCategories = null; 152 153 if(!hasExecutionErrors()) { 154 if(parentContentCategory == null) { 155 contentCategories = contentControl.getContentCategories(contentCatalog); 156 } else { 157 contentCategories = contentControl.getContentCategoriesByParentContentCategory(parentContentCategory); 158 } 159 } 160 161 return contentCategories; 162 } 163 164 @Override 165 protected BaseResult getResult(Collection<ContentCategory> entities) { 166 var result = ContentResultFactory.getGetContentCategoriesResult(); 167 168 if(entities != null) { 169 var userVisit = getUserVisit(); 170 171 result.setContentCatalog(contentControl.getContentCatalogTransfer(userVisit, contentCatalog)); 172 173 if(parentContentCategory != null) { 174 result.setParentContentCategory(contentControl.getContentCategoryTransfer(userVisit, parentContentCategory)); 175 } 176 177 if(session.hasLimit(ContentCategoryFactory.class)) { 178 result.setContentCategoryCount(getTotalEntities()); 179 } 180 181 result.setContentCategories(contentControl.getContentCategoryTransfers(userVisit, entities)); 182 } 183 184 return result; 185 } 186 187}