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