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