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.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 javax.enterprise.context.RequestScoped;
035
036@RequestScoped
037public class SecurityRoleControl
038        extends BaseModelControl {
039
040    /** Creates a new instance of SecurityRoleControl */
041    protected SecurityRoleControl() {
042        super();
043    }
044
045    // --------------------------------------------------------------------------------
046    //   Security Role Searches
047    // --------------------------------------------------------------------------------
048
049    public List<SecurityRoleResultTransfer> getSecurityRoleResultTransfers(UserVisit userVisit, UserVisitSearch userVisitSearch) {
050        var search = userVisitSearch.getSearch();
051        var securityRoleResultTransfers = new ArrayList<SecurityRoleResultTransfer>();
052        var includeSecurityRole = false;
053
054        var options = session.getOptions();
055        if(options != null) {
056            includeSecurityRole = options.contains(SearchOptions.SecurityRoleResultIncludeSecurityRole);
057        }
058
059        try {
060            var securityControl = Session.getModelController(SecurityControl.class);
061            var ps = SearchResultFactory.getInstance().prepareStatement(
062                    "SELECT eni_entityuniqueid " +
063                            "FROM searchresults, entityinstances " +
064                            "WHERE srchr_srch_searchid = ? AND srchr_eni_entityinstanceid = eni_entityinstanceid " +
065                            "ORDER BY srchr_sortorder, srchr_eni_entityinstanceid " +
066                            "_LIMIT_");
067
068            ps.setLong(1, search.getPrimaryKey().getEntityId());
069
070            try (var rs = ps.executeQuery()) {
071                while(rs.next()) {
072                    var securityRole = SecurityRoleFactory.getInstance().getEntityFromPK(EntityPermission.READ_ONLY, new SecurityRolePK(rs.getLong(1)));
073                    var securityRoleDetail = securityRole.getLastDetail();
074
075                    securityRoleResultTransfers.add(new SecurityRoleResultTransfer(securityRoleDetail.getSecurityRoleName(),
076                            securityRoleDetail.getSecurityRoleGroup().getLastDetail().getSecurityRoleGroupName(),
077                            includeSecurityRole ? securityControl.getSecurityRoleTransfer(userVisit, securityRole) : null));
078                }
079            } catch (SQLException se) {
080                throw new PersistenceDatabaseException(se);
081            }
082        } catch (SQLException se) {
083            throw new PersistenceDatabaseException(se);
084        }
085
086        return securityRoleResultTransfers;
087    }
088
089}