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.edit.ContentCatalogEdit; 020import com.echothree.control.user.content.common.edit.ContentEditFactory; 021import com.echothree.control.user.content.common.form.EditContentCatalogForm; 022import com.echothree.control.user.content.common.result.ContentResultFactory; 023import com.echothree.control.user.content.common.result.EditContentCatalogResult; 024import com.echothree.control.user.content.common.spec.ContentCatalogSpec; 025import com.echothree.model.control.content.server.control.ContentControl; 026import com.echothree.model.control.offer.server.control.OfferControl; 027import com.echothree.model.control.offer.server.control.OfferUseControl; 028import com.echothree.model.control.offer.server.control.SourceControl; 029import com.echothree.model.control.offer.server.control.UseControl; 030import com.echothree.model.control.party.common.PartyTypes; 031import com.echothree.model.control.party.server.control.PartyControl; 032import com.echothree.model.control.security.common.SecurityRoleGroups; 033import com.echothree.model.control.security.common.SecurityRoles; 034import com.echothree.model.data.content.server.entity.ContentCatalog; 035import com.echothree.model.data.content.server.entity.ContentCollection; 036import com.echothree.model.data.offer.server.entity.OfferUse; 037import com.echothree.model.data.user.common.pk.UserVisitPK; 038import com.echothree.util.common.command.EditMode; 039import com.echothree.util.common.message.ExecutionErrors; 040import com.echothree.util.common.validation.FieldDefinition; 041import com.echothree.util.common.validation.FieldType; 042import com.echothree.util.server.control.BaseAbstractEditCommand; 043import com.echothree.util.server.control.CommandSecurityDefinition; 044import com.echothree.util.server.control.PartyTypeDefinition; 045import com.echothree.util.server.control.SecurityRoleDefinition; 046import com.echothree.util.server.persistence.Session; 047import java.util.Arrays; 048import java.util.Collections; 049import java.util.List; 050import javax.enterprise.context.RequestScoped; 051 052@RequestScoped 053public class EditContentCatalogCommand 054 extends BaseAbstractEditCommand<ContentCatalogSpec, ContentCatalogEdit, EditContentCatalogResult, ContentCatalog, ContentCatalog> { 055 056 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 057 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 058 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 059 060 static { 061 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 062 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 063 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 064 new SecurityRoleDefinition(SecurityRoleGroups.ContentCatalog.name(), SecurityRoles.Edit.name()) 065 ))) 066 ))); 067 068 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 069 new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null) 071 )); 072 073 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 074 new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null), 075 new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null), 076 new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null), 077 new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null), 078 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 079 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 080 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 081 )); 082 } 083 084 /** Creates a new instance of EditContentCatalogCommand */ 085 public EditContentCatalogCommand() { 086 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 087 } 088 089 @Override 090 public EditContentCatalogResult getResult() { 091 return ContentResultFactory.getEditContentCatalogResult(); 092 } 093 094 @Override 095 public ContentCatalogEdit getEdit() { 096 return ContentEditFactory.getContentCatalogEdit(); 097 } 098 099 ContentCollection contentCollection = null; 100 101 @Override 102 public ContentCatalog getEntity(EditContentCatalogResult result) { 103 var contentControl = Session.getModelController(ContentControl.class); 104 ContentCatalog contentCatalog = null; 105 var contentCollectionName = spec.getContentCollectionName(); 106 107 contentCollection = contentControl.getContentCollectionByName(contentCollectionName); 108 109 if(contentCollection != null) { 110 var contentCatalogName = spec.getContentCatalogName(); 111 112 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 113 contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 114 } else { // EditMode.UPDATE 115 contentCatalog = contentControl.getContentCatalogByNameForUpdate(contentCollection, contentCatalogName); 116 } 117 118 if(contentCatalog != null) { 119 result.setContentCatalog(contentControl.getContentCatalogTransfer(getUserVisit(), contentCatalog)); 120 } else { 121 addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName); 125 } 126 127 return contentCatalog; 128 } 129 130 @Override 131 public ContentCatalog getLockEntity(ContentCatalog contentCatalog) { 132 return contentCatalog; 133 } 134 135 @Override 136 public void fillInResult(EditContentCatalogResult result, ContentCatalog contentCatalog) { 137 var contentControl = Session.getModelController(ContentControl.class); 138 139 result.setContentCatalog(contentControl.getContentCatalogTransfer(getUserVisit(), contentCatalog)); 140 } 141 142 @Override 143 public void doLock(ContentCatalogEdit edit, ContentCatalog contentCatalog) { 144 var contentControl = Session.getModelController(ContentControl.class); 145 var sourceControl = Session.getModelController(SourceControl.class); 146 var contentCatalogDescription = contentControl.getContentCatalogDescription(contentCatalog, getPreferredLanguage()); 147 var contentCatalogDetail = contentCatalog.getLastDetail(); 148 var offerUse = contentCatalogDetail.getDefaultOfferUse(); 149 var defaultOfferUseDetail = offerUse.getLastDetail(); 150 var sources = defaultOfferUse == null ? null : sourceControl.getSourcesByOfferUse(defaultOfferUse); // List of Sources if there are any for the OfferUse 151 var defaultSourceName = sources == null ? null : sources.iterator().next().getLastDetail().getSourceName(); // From the List, the first available Source's SourceName 152 153 edit.setContentCatalogName(contentCatalogDetail.getContentCatalogName()); 154 edit.setDefaultOfferName(defaultSourceName == null ? (defaultOfferUseDetail == null? null: defaultOfferUseDetail.getOffer().getLastDetail().getOfferName()) : null); 155 edit.setDefaultUseName(defaultSourceName == null ? (defaultOfferUseDetail == null ? null : defaultOfferUseDetail.getUse().getLastDetail().getUseName()) : null); 156 edit.setDefaultSourceName(defaultSourceName); 157 edit.setIsDefault(contentCatalogDetail.getIsDefault().toString()); 158 edit.setSortOrder(contentCatalogDetail.getSortOrder().toString()); 159 160 if(contentCatalogDescription != null) { 161 edit.setDescription(contentCatalogDescription.getDescription()); 162 } 163 } 164 165 OfferUse defaultOfferUse = null; 166 167 @Override 168 public void canUpdate(ContentCatalog contentCatalog) { 169 var contentControl = Session.getModelController(ContentControl.class); 170 var contentCatalogName = edit.getContentCatalogName(); 171 var duplicateContentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName); 172 173 if(duplicateContentCatalog == null || contentCatalog.equals(duplicateContentCatalog)) { 174 var offerControl = Session.getModelController(OfferControl.class); 175 var defaultOfferName = edit.getDefaultOfferName(); 176 var defaultUseName = edit.getDefaultUseName(); 177 var defaultSourceName = edit.getDefaultSourceName(); 178 179 if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) { 180 var defaultOffer = offerControl.getOfferByName(defaultOfferName); 181 182 if(defaultOffer != null) { 183 var useControl = Session.getModelController(UseControl.class); 184 var defaultUse = useControl.getUseByName(defaultUseName); 185 186 if(defaultUse != null) { 187 var offerUseControl = Session.getModelController(OfferUseControl.class); 188 defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse); 189 190 if(defaultOfferUse == null) { 191 addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name()); 192 } 193 } else { 194 addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName); 195 } 196 } else { 197 addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName); 198 } 199 } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) { 200 var sourceControl = Session.getModelController(SourceControl.class); 201 var source = sourceControl.getSourceByName(defaultSourceName); 202 203 if(source != null) { 204 defaultOfferUse = source.getLastDetail().getOfferUse(); 205 } else { 206 addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName); 207 } 208 } else { 209 addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name()); 210 } 211 212 if(defaultOfferUse != null) { 213 var partyControl = Session.getModelController(PartyControl.class); 214 var defaultOffer = defaultOfferUse.getLastDetail().getOffer(); 215 var defaultPartyDepartment = partyControl.getPartyDepartment(defaultOffer.getLastDetail().getDepartmentParty()); 216 var defaultPartyDivision = partyControl.getPartyDivision(defaultPartyDepartment.getDivisionParty()); 217 var defaultPartyCompany = partyControl.getPartyCompany(defaultPartyDivision.getCompanyParty()); 218 219 var catalogOffer = contentCatalog.getLastDetail().getDefaultOfferUse().getLastDetail().getOffer(); 220 var catalogPartyDepartment = partyControl.getPartyDepartment(catalogOffer.getLastDetail().getDepartmentParty()); 221 var catalogPartyDivision = partyControl.getPartyDivision(catalogPartyDepartment.getDivisionParty()); 222 var catalogPartyCompany = partyControl.getPartyCompany(catalogPartyDivision.getCompanyParty()); 223 224 if(!defaultPartyCompany.equals(catalogPartyCompany)) { 225 addExecutionError(ExecutionErrors.InvalidOfferCompany.name()); 226 } 227 } 228 } else { 229 addExecutionError(ExecutionErrors.DuplicateContentCatalogName.name(), contentCatalogName); 230 } 231 } 232 233 @Override 234 public void doUpdate(ContentCatalog contentCatalog) { 235 var contentControl = Session.getModelController(ContentControl.class); 236 var partyPK = getPartyPK(); 237 var contentCatalogDetailValue = contentControl.getContentCatalogDetailValueForUpdate(contentCatalog); 238 var contentCatalogDescription = contentControl.getContentCatalogDescriptionForUpdate(contentCatalog, getPreferredLanguage()); 239 var description = edit.getDescription(); 240 241 contentCatalogDetailValue.setContentCatalogName(edit.getContentCatalogName()); 242 contentCatalogDetailValue.setDefaultOfferUsePK(defaultOfferUse.getPrimaryKey()); 243 contentCatalogDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 244 contentCatalogDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 245 246 contentControl.updateContentCatalogFromValue(contentCatalogDetailValue, partyPK); 247 248 if(contentCatalogDescription == null && description != null) { 249 contentControl.createContentCatalogDescription(contentCatalog, getPreferredLanguage(), description, partyPK); 250 } else if(contentCatalogDescription != null && description == null) { 251 contentControl.deleteContentCatalogDescription(contentCatalogDescription, partyPK); 252 } else if(contentCatalogDescription != null && description != null) { 253 var contentCatalogDescriptionValue = contentControl.getContentCatalogDescriptionValue(contentCatalogDescription); 254 255 contentCatalogDescriptionValue.setDescription(description); 256 contentControl.updateContentCatalogDescriptionFromValue(contentCatalogDescriptionValue, partyPK); 257 } 258 } 259 260} 261