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.GetContentCatalogItemsForm; 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.control.party.common.PartyTypes; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.data.content.server.entity.ContentCatalog; 027import com.echothree.model.data.content.server.entity.ContentCatalogItem; 028import com.echothree.model.data.content.server.entity.ContentCollection; 029import com.echothree.model.data.content.server.factory.ContentCatalogItemFactory; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 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.control.BasePaginatedMultipleEntitiesCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.persistence.Session; 041import java.util.Collection; 042import java.util.List; 043import javax.enterprise.context.Dependent; 044 045@Dependent 046public class GetContentCatalogItemsCommand 047 extends BasePaginatedMultipleEntitiesCommand<ContentCatalogItem, GetContentCatalogItemsForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.ContentCatalogItem.name(), SecurityRoles.List.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null), 062 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null), 065 new FieldDefinition("AssociateName", FieldType.STRING, false, null, null), 066 new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null) 067 ); 068 } 069 070 /** Creates a new instance of GetContentCatalogItemsCommand */ 071 public GetContentCatalogItemsCommand() { 072 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 073 } 074 075 private ContentCatalog contentCatalog; 076 077 @Override 078 protected void handleForm() { 079 var contentWebAddressName = form.getContentWebAddressName(); 080 var contentCollectionName = form.getContentCollectionName(); 081 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 082 083 if(parameterCount == 1) { 084 var contentControl = Session.getModelController(ContentControl.class); 085 ContentCollection contentCollection = null; 086 087 if(contentWebAddressName != null) { 088 var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 089 090 if(contentWebAddress != null) { 091 contentCollection = contentWebAddress.getLastDetail().getContentCollection(); 092 } else { 093 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 094 } 095 } else { 096 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 097 098 if(contentCollection == null) { 099 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 100 } 101 } 102 103 if(!hasExecutionErrors()) { 104 var contentCatalogName = form.getContentCatalogName(); 105 106 if(contentCatalogName == null) { 107 contentCatalog = contentControl.getDefaultContentCatalog(contentCollection); 108 109 if(contentCatalog == null) { 110 addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(), 111 contentCollection.getLastDetail().getContentCollectionName()); 112 } 113 } else { 114 contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 115 116 if(contentCatalog == null) { 117 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), 118 contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName); 119 } 120 } 121 } 122 } else { 123 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 124 } 125 126 if(!hasExecutionErrors()) { 127 AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(), 128 contentCatalog.getPrimaryKey(), getPartyPK()); 129 130 } 131 } 132 133 @Override 134 protected Long getTotalEntities() { 135 var contentControl = Session.getModelController(ContentControl.class); 136 137 return hasExecutionErrors() ? null : 138 contentControl.countContentCatalogItemsByContentCatalog(contentCatalog); 139 } 140 141 @Override 142 protected Collection<ContentCatalogItem> getEntities() { 143 Collection<ContentCatalogItem> contentCatalogItems = null; 144 145 if(!hasExecutionErrors()) { 146 var contentControl = Session.getModelController(ContentControl.class); 147 148 contentCatalogItems = contentControl.getContentCatalogItemsByContentCatalog(contentCatalog); 149 } 150 151 return contentCatalogItems; 152 } 153 154 @Override 155 protected BaseResult getResult(Collection<ContentCatalogItem> entities) { 156 var result = ContentResultFactory.getGetContentCatalogItemsResult(); 157 158 if(entities != null) { 159 var contentControl = Session.getModelController(ContentControl.class); 160 var userVisit = getUserVisit(); 161 162 result.setContentCatalog(contentControl.getContentCatalogTransfer(userVisit, contentCatalog)); 163 164 if(session.hasLimit(ContentCatalogItemFactory.class)) { 165 result.setContentCatalogItemCount(getTotalEntities()); 166 } 167 168 result.setContentCatalogItems(contentControl.getContentCatalogItemTransfers(userVisit, entities)); 169 } 170 171 return result; 172 } 173 174}