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.ContentEditFactory;
020import com.echothree.control.user.content.common.edit.ContentPageEdit;
021import com.echothree.control.user.content.common.form.EditContentPageForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentPageResult;
024import com.echothree.control.user.content.common.spec.ContentPageSpec;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.content.server.entity.ContentPage;
030import com.echothree.model.data.content.server.entity.ContentPageLayout;
031import com.echothree.model.data.content.server.entity.ContentSection;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.EditMode;
037import com.echothree.util.server.control.BaseAbstractEditCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045import javax.enterprise.context.RequestScoped;
046
047@RequestScoped
048public class EditContentPageCommand
049        extends BaseAbstractEditCommand<ContentPageSpec, ContentPageEdit, EditContentPageResult, ContentPage, ContentPage> {
050    
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
053    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
054    
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
059                        new SecurityRoleDefinition(SecurityRoleGroups.ContentPage.name(), SecurityRoles.Edit.name())
060                        )))
061                )));
062        
063        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
064                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null)
067                ));
068        
069        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
070                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ContentPageLayoutName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
073                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
074                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
075                ));
076    }
077    
078    /** Creates a new instance of EditContentPageCommand */
079    public EditContentPageCommand() {
080        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    public EditContentPageResult getResult() {
085        return ContentResultFactory.getEditContentPageResult();
086    }
087    
088    @Override
089    public ContentPageEdit getEdit() {
090        return ContentEditFactory.getContentPageEdit();
091    }
092    
093    ContentSection contentSection = null;
094    
095    @Override
096    public ContentPage getEntity(EditContentPageResult result) {
097        var contentControl = Session.getModelController(ContentControl.class);
098        ContentPage contentPage = null;
099        var contentCollectionName = spec.getContentCollectionName();
100        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
101        
102        if(contentCollection != null) {
103            var contentSectionName = spec.getContentSectionName();
104            
105            contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
106            
107            if(contentSection != null) {
108                var contentPageName = spec.getContentPageName();
109                
110                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
111                    contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
112                } else { // EditMode.UPDATE
113                    contentPage = contentControl.getContentPageByNameForUpdate(contentSection, contentPageName);
114                }
115
116                if(contentPage != null) {
117                    result.setContentPage(contentControl.getContentPageTransfer(getUserVisit(), contentPage));
118                } else {
119                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
120                }
121            } else {
122                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
123            }
124        } else {
125            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
126        }
127
128        return contentPage;
129    }
130    
131    @Override
132    public ContentPage getLockEntity(ContentPage contentPage) {
133        return contentPage;
134    }
135    
136    @Override
137    public void fillInResult(EditContentPageResult result, ContentPage contentPage) {
138        var contentControl = Session.getModelController(ContentControl.class);
139        
140        result.setContentPage(contentControl.getContentPageTransfer(getUserVisit(), contentPage));
141    }
142    
143    @Override
144    public void doLock(ContentPageEdit edit, ContentPage contentPage) {
145        var contentControl = Session.getModelController(ContentControl.class);
146        var contentPageDescription = contentControl.getContentPageDescription(contentPage, getPreferredLanguage());
147        var contentPageDetail = contentPage.getLastDetail();
148
149        edit.setContentPageName(contentPageDetail.getContentPageName());
150        edit.setContentPageLayoutName(contentPageDetail.getContentPageLayout().getLastDetail().getContentPageLayoutName());
151        edit.setIsDefault(contentPageDetail.getIsDefault().toString());
152        edit.setSortOrder(contentPageDetail.getSortOrder().toString());
153
154        if(contentPageDescription != null) {
155            edit.setDescription(contentPageDescription.getDescription());
156        }
157    }
158    
159    ContentPageLayout contentPageLayout = null;
160    
161    @Override
162    public void canUpdate(ContentPage contentPage) {
163        var contentControl = Session.getModelController(ContentControl.class);
164        var contentPageName = edit.getContentPageName();
165        var duplicateContentPage = contentControl.getContentPageByName(contentSection, contentPageName);
166
167        if(duplicateContentPage == null || contentPage.equals(duplicateContentPage)) {
168            var contentPageLayoutName = edit.getContentPageLayoutName();
169            
170            contentPageLayout = contentControl.getContentPageLayoutByName(contentPageLayoutName);
171
172            if(contentPageLayout == null) {
173                addExecutionError(ExecutionErrors.UnknownContentPageLayoutName.name(), contentPageLayoutName);
174            }
175        } else {
176            addExecutionError(ExecutionErrors.DuplicateContentPageName.name(), contentPageName);
177        }
178    }
179    
180    @Override
181    public void doUpdate(ContentPage contentPage) {
182        var contentControl = Session.getModelController(ContentControl.class);
183        var partyPK = getPartyPK();
184        var contentPageDetailValue = contentControl.getContentPageDetailValueForUpdate(contentPage);
185        var contentPageDescription = contentControl.getContentPageDescriptionForUpdate(contentPage, getPreferredLanguage());
186        var description = edit.getDescription();
187
188        contentPageDetailValue.setContentPageName(edit.getContentPageName());
189        contentPageDetailValue.setContentPageLayoutPK(contentPageLayout.getPrimaryKey());
190        contentPageDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
191        contentPageDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
192
193        contentControl.updateContentPageFromValue(contentPageDetailValue, partyPK);
194
195        if(contentPageDescription == null && description != null) {
196            contentControl.createContentPageDescription(contentPage, getPreferredLanguage(), description, partyPK);
197        } else if(contentPageDescription != null && description == null) {
198            contentControl.deleteContentPageDescription(contentPageDescription, partyPK);
199        } else if(contentPageDescription != null && description != null) {
200            var contentPageDescriptionValue = contentControl.getContentPageDescriptionValue(contentPageDescription);
201
202            contentPageDescriptionValue.setDescription(description);
203            contentControl.updateContentPageDescriptionFromValue(contentPageDescriptionValue, partyPK);
204        }
205    }
206    
207}