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