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