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