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.model.control.security.server.control;
018
019import com.echothree.model.control.search.common.SearchOptions;
020import com.echothree.model.control.security.common.transfer.SecurityRoleResultTransfer;
021import com.echothree.model.data.search.server.entity.UserVisitSearch;
022import com.echothree.model.data.search.server.factory.SearchResultFactory;
023import com.echothree.model.data.security.common.pk.SecurityRolePK;
024import com.echothree.model.data.security.server.factory.SecurityRoleFactory;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import com.echothree.util.common.exception.PersistenceDatabaseException;
027import com.echothree.util.server.control.BaseModelControl;
028import javax.enterprise.inject.spi.CDI;
029import com.echothree.util.server.persistence.EntityPermission;
030import com.echothree.util.server.persistence.Session;
031import java.sql.SQLException;
032import java.util.ArrayList;
033import java.util.List;
034import com.echothree.util.server.cdi.CommandScope;
035import javax.inject.Inject;
036
037@CommandScope
038public class SecurityRoleControl
039        extends BaseModelControl {
040
041    @Inject
042    protected SecurityControl securityControl;
043
044    /** Creates a new instance of SecurityRoleControl */
045    protected SecurityRoleControl() {
046        super();
047    }
048
049    // --------------------------------------------------------------------------------
050    //   Security Role Searches
051    // --------------------------------------------------------------------------------
052
053    @Inject
054    protected SearchResultFactory searchResultFactory;
055
056    @Inject
057    protected SecurityRoleFactory securityRoleFactory;
058
059    public List<SecurityRoleResultTransfer> getSecurityRoleResultTransfers(UserVisit userVisit, UserVisitSearch userVisitSearch) {
060        var search = userVisitSearch.getSearch();
061        var securityRoleResultTransfers = new ArrayList<SecurityRoleResultTransfer>();
062        var includeSecurityRole = false;
063
064        var options = session.getOptions();
065        if(options != null) {
066            includeSecurityRole = options.contains(SearchOptions.SecurityRoleResultIncludeSecurityRole);
067        }
068
069        try {
070            var ps = searchResultFactory.prepareStatement(
071                    """
072                    SELECT eni_entityuniqueid
073                    FROM searchresults, entityinstances
074                    WHERE srchr_srch_searchid = ? AND srchr_eni_entityinstanceid = eni_entityinstanceid
075                    ORDER BY srchr_sortorder, srchr_eni_entityinstanceid
076                    _LIMIT_
077                    """);
078
079            ps.setLong(1, search.getPrimaryKey().getEntityId());
080
081            try (var rs = ps.executeQuery()) {
082                while(rs.next()) {
083                    var securityRole = securityRoleFactory.getEntityFromPK(EntityPermission.READ_ONLY, new SecurityRolePK(rs.getLong(1)));
084                    var securityRoleDetail = securityRole.getLastDetail();
085
086                    securityRoleResultTransfers.add(new SecurityRoleResultTransfer(securityRoleDetail.getSecurityRoleName(),
087                            securityRoleDetail.getSecurityRoleGroup().getLastDetail().getSecurityRoleGroupName(),
088                            includeSecurityRole ? securityControl.getSecurityRoleTransfer(userVisit, securityRole) : null));
089                }
090            } catch (SQLException se) {
091                throw new PersistenceDatabaseException(se);
092            }
093        } catch (SQLException se) {
094            throw new PersistenceDatabaseException(se);
095        }
096
097        return securityRoleResultTransfers;
098    }
099
100}