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.ContentEditFactory;
020import com.echothree.control.user.content.common.edit.ContentPageLayoutDescriptionEdit;
021import com.echothree.control.user.content.common.result.ContentResultFactory;
022import com.echothree.control.user.content.common.result.EditContentPageLayoutDescriptionResult;
023import com.echothree.control.user.content.common.spec.ContentPageLayoutDescriptionSpec;
024import com.echothree.model.control.content.server.control.ContentControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.content.server.entity.ContentPageLayout;
030import com.echothree.model.data.content.server.entity.ContentPageLayoutDescription;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class EditContentPageLayoutDescriptionCommand
044        extends BaseAbstractEditCommand<ContentPageLayoutDescriptionSpec, ContentPageLayoutDescriptionEdit, EditContentPageLayoutDescriptionResult, ContentPageLayoutDescription, ContentPageLayout> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
048    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.ContentPageLayout.name(), SecurityRoles.Description.name())
055                ))
056        ));
057        
058        SPEC_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("ContentPageLayoutName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
061        );
062        
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
065        );
066    }
067    
068    /** Creates a new instance of EditContentPageLayoutDescriptionCommand */
069    public EditContentPageLayoutDescriptionCommand() {
070        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
071    }
072    
073    @Inject
074    ContentControl contentControl;
075
076    @Inject
077    PartyControl partyControl;
078
079    @Override
080    public EditContentPageLayoutDescriptionResult getResult() {
081        return ContentResultFactory.getEditContentPageLayoutDescriptionResult();
082    }
083
084    @Override
085    public ContentPageLayoutDescriptionEdit getEdit() {
086        return ContentEditFactory.getContentPageLayoutDescriptionEdit();
087    }
088
089    @Override
090    public ContentPageLayoutDescription getEntity(EditContentPageLayoutDescriptionResult result) {
091        ContentPageLayoutDescription contentPageLayoutDescription = null;
092        var contentPageLayoutName = spec.getContentPageLayoutName();
093        var contentPageLayout = contentControl.getContentPageLayoutByName(contentPageLayoutName);
094
095        if(contentPageLayout != null) {
096            var languageIsoName = spec.getLanguageIsoName();
097            var language = partyControl.getLanguageByIsoName(languageIsoName);
098
099            if(language != null) {
100                contentPageLayoutDescription = contentControl.getContentPageLayoutDescription(contentPageLayout, language,
101                        editModeToEntityPermission(editMode));
102
103                if(contentPageLayoutDescription == null) {
104                    addExecutionError(ExecutionErrors.UnknownContentPageLayoutDescription.name(),
105                            contentPageLayout.getLastDetail().getContentPageLayoutName(), language.getLanguageIsoName());
106                }
107            } else {
108                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
109            }
110        } else {
111            addExecutionError(ExecutionErrors.UnknownContentPageLayoutName.name(), contentPageLayoutName);
112        }
113
114        return contentPageLayoutDescription;
115    }
116
117    @Override
118    public ContentPageLayout getLockEntity(ContentPageLayoutDescription contentPageLayoutDescription) {
119        return contentPageLayoutDescription.getContentPageLayout();
120    }
121
122    @Override
123    public void fillInResult(EditContentPageLayoutDescriptionResult result, ContentPageLayoutDescription contentPageLayoutDescription) {
124        result.setContentPageLayoutDescription(contentControl.getContentPageLayoutDescriptionTransfer(getUserVisit(), contentPageLayoutDescription));
125    }
126
127    @Override
128    public void doLock(ContentPageLayoutDescriptionEdit edit, ContentPageLayoutDescription contentPageLayoutDescription) {
129        edit.setDescription(contentPageLayoutDescription.getDescription());
130    }
131
132    @Override
133    public void doUpdate(ContentPageLayoutDescription contentPageLayoutDescription) {
134        var contentPageLayoutDescriptionValue = contentControl.getContentPageLayoutDescriptionValue(contentPageLayoutDescription);
135
136        contentPageLayoutDescriptionValue.setDescription(edit.getDescription());
137
138        contentControl.updateContentPageLayoutDescriptionFromValue(contentPageLayoutDescriptionValue, getPartyPK());
139    }
140    
141}