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.core.server.command;
018
019import com.echothree.control.user.core.common.form.GetLinksForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.model.control.core.server.control.EntityInstanceControl;
022import com.echothree.model.control.core.server.control.LinkControl;
023import com.echothree.model.control.core.server.control.LinkTypeControl;
024import com.echothree.model.control.core.server.logic.LinkLogic;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.core.server.entity.Link;
029import com.echothree.model.data.core.server.factory.LinkFactory;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.Collection;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041import org.jooq.Condition;
042
043@Dependent
044public class GetLinksCommand
045        extends BasePaginatedMultipleEntitiesCommand<Link, GetLinksForm> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_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.Link.name(), SecurityRoles.List.name())
055                ))
056        ));
057
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("LinkTypeName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("SourceEntityRef", FieldType.ENTITY_REF, false, null, null),
061                new FieldDefinition("SourceUuid", FieldType.UUID, false, null, null),
062                new FieldDefinition("TargetEntityRef", FieldType.ENTITY_REF, false, null, null),
063                new FieldDefinition("TargetUuid", FieldType.UUID, false, null, null)
064        );
065    }
066
067    @Inject
068    EntityInstanceControl entityInstanceControl;
069
070    @Inject
071    LinkControl linkControl;
072
073    @Inject
074    LinkTypeControl linkTypeControl;
075
076    @Inject
077    LinkLogic linkLogic;
078
079    /** Creates a new instance of GetLinksCommand */
080    public GetLinksCommand() {
081        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
082    }
083
084    private Condition condition;
085    private LinkLogic.Definition definition;
086
087    @Override
088    protected void handleForm() {
089        definition = linkLogic.getDefinition(this, form, true);
090
091        if(!hasExecutionErrors()) {
092            condition = linkControl.getLinkFilter(definition.linkType(), definition.sourceEntityInstance(), definition.targetEntityInstance());
093        }
094    }
095
096    @Override
097    protected Long getTotalEntities() {
098        return hasExecutionErrors() ? null : linkControl.countLinks(condition);
099    }
100
101    @Override
102    protected Collection<Link> getEntities() {
103        return hasExecutionErrors() ? null : linkControl.getLinks(condition);
104    }
105
106    @Override
107    protected BaseResult getResult(Collection<Link> entities) {
108        var result = CoreResultFactory.getGetLinksResult();
109
110        if(entities != null) {
111            var userVisit = getUserVisit();
112
113            if(definition.linkType() != null) {
114                result.setLinkType(linkTypeControl.getLinkTypeTransfer(userVisit, definition.linkType()));
115            }
116
117            if(definition.sourceEntityInstance() != null) {
118                result.setSourceEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit,
119                        definition.sourceEntityInstance(), false, false, false, false));
120            }
121
122            if(definition.targetEntityInstance() != null) {
123                result.setTargetEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit,
124                        definition.targetEntityInstance(), false, false, false, false));
125            }
126
127            if(session.hasLimit(LinkFactory.class)) {
128                result.setLinkCount(getTotalEntities());
129            }
130
131            result.setLinks(linkControl.getLinkTransfers(userVisit, entities));
132        }
133
134        return result;
135    }
136
137}