001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.GetContentSectionForm; 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.core.common.EventTypes; 024import com.echothree.model.data.content.server.entity.ContentCollection; 025import com.echothree.model.data.content.server.entity.ContentSection; 026import com.echothree.model.data.user.common.pk.UserVisitPK; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.server.control.BaseSingleEntityCommand; 032import com.echothree.util.server.persistence.Session; 033import java.util.Arrays; 034import java.util.Collections; 035import java.util.List; 036import javax.enterprise.context.RequestScoped; 037 038@RequestScoped 039public class GetContentSectionCommand 040 extends BaseSingleEntityCommand<ContentSection, GetContentSectionForm> { 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 = Collections.unmodifiableList(Arrays.asList( 047 new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null), 048 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null), 049 new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null), 051 new FieldDefinition("AssociateName", FieldType.STRING, false, null, null), 052 new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null) 053 )); 054 } 055 056 /** Creates a new instance of GetContentSectionCommand */ 057 public GetContentSectionCommand() { 058 super(null, FORM_FIELD_DEFINITIONS, true); 059 } 060 061 @Override 062 protected ContentSection getEntity() { 063 var contentWebAddressName = form.getContentWebAddressName(); 064 var contentCollectionName = form.getContentCollectionName(); 065 var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1); 066 ContentSection contentSection = null; 067 068 if(parameterCount == 1) { 069 var contentControl = Session.getModelController(ContentControl.class); 070 ContentCollection contentCollection = null; 071 072 if(contentWebAddressName != null) { 073 var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName); 074 075 if(contentWebAddress != null) { 076 contentCollection = contentWebAddress.getLastDetail().getContentCollection(); 077 } else { 078 addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName); 079 } 080 } else { 081 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 082 083 if(contentCollection == null) { 084 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 085 } 086 } 087 088 if(!hasExecutionErrors()) { 089 var contentSectionName = form.getContentSectionName(); 090 var partyPK = getPartyPK(); 091 var userVisit = getUserVisitForUpdate(); 092 093 contentSection = contentSectionName == null ? contentControl.getDefaultContentSection(contentCollection) 094 : contentControl.getContentSectionByName(contentCollection, contentSectionName); 095 096 if(contentSection != null) { 097 AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, userVisit, contentSection.getPrimaryKey(), partyPK); 098 099 if(!hasExecutionErrors()) { 100 sendEvent(contentSection.getPrimaryKey(), EventTypes.READ, null, null, partyPK); 101 } 102 } else { 103 addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentCollection.getLastDetail().getContentCollectionName(), contentSectionName); 104 } 105 } 106 } else { 107 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 108 } 109 110 return contentSection; 111 } 112 113 @Override 114 protected BaseResult getResult(ContentSection contentSection) { 115 var result = ContentResultFactory.getGetContentSectionResult(); 116 117 if(contentSection != null) { 118 var contentControl = Session.getModelController(ContentControl.class); 119 120 result.setContentSection(contentControl.getContentSectionTransfer(getUserVisit(), contentSection)); 121 } 122 123 return result; 124 } 125}