|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.api.query.dao; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import javax.ejb.Local; |
| 25 | +import javax.inject.Inject; |
| 26 | + |
| 27 | +import org.apache.log4j.Logger; |
| 28 | +import org.springframework.stereotype.Component; |
| 29 | + |
| 30 | +import org.apache.cloudstack.acl.AclGroup; |
| 31 | +import org.apache.cloudstack.acl.AclGroupAccountMapVO; |
| 32 | +import org.apache.cloudstack.acl.dao.AclGroupAccountMapDao; |
| 33 | +import org.apache.cloudstack.api.response.AclGroupResponse; |
| 34 | +import org.apache.cloudstack.api.response.AclRoleResponse; |
| 35 | +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; |
| 36 | + |
| 37 | +import com.cloud.api.query.vo.AclGroupJoinVO; |
| 38 | +import com.cloud.user.AccountManager; |
| 39 | +import com.cloud.utils.db.GenericDaoBase; |
| 40 | +import com.cloud.utils.db.SearchBuilder; |
| 41 | +import com.cloud.utils.db.SearchCriteria; |
| 42 | + |
| 43 | +@Component |
| 44 | +@Local(value = {AclGroupJoinDao.class}) |
| 45 | +public class AclGroupJoinDaoImpl extends GenericDaoBase<AclGroupJoinVO, Long> implements AclGroupJoinDao { |
| 46 | + public static final Logger s_logger = Logger.getLogger(AclGroupJoinDaoImpl.class); |
| 47 | + |
| 48 | + |
| 49 | + private final SearchBuilder<AclGroupJoinVO> grpIdSearch; |
| 50 | + private final SearchBuilder<AclGroupJoinVO> grpSearch; |
| 51 | + @Inject |
| 52 | + public AccountManager _accountMgr; |
| 53 | + @Inject |
| 54 | + public ConfigurationDao _configDao; |
| 55 | + @Inject |
| 56 | + public AclGroupAccountMapDao _grpAccountDao; |
| 57 | + |
| 58 | + protected AclGroupJoinDaoImpl() { |
| 59 | + |
| 60 | + grpSearch = createSearchBuilder(); |
| 61 | + grpSearch.and("idIN", grpSearch.entity().getId(), SearchCriteria.Op.IN); |
| 62 | + grpSearch.done(); |
| 63 | + |
| 64 | + grpIdSearch = createSearchBuilder(); |
| 65 | + grpIdSearch.and("id", grpIdSearch.entity().getId(), SearchCriteria.Op.EQ); |
| 66 | + grpIdSearch.done(); |
| 67 | + |
| 68 | + _count = "select count(distinct id) from acl_group_view WHERE "; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + @Override |
| 74 | + public AclGroupResponse newAclGroupResponse(AclGroupJoinVO group) { |
| 75 | + |
| 76 | + AclGroupResponse response = new AclGroupResponse(); |
| 77 | + response.setId(group.getUuid()); |
| 78 | + response.setName(group.getName()); |
| 79 | + response.setDescription(group.getDescription()); |
| 80 | + response.setDomainId(group.getDomainUuid()); |
| 81 | + response.setDomainName(group.getName()); |
| 82 | + if (group.getAccountId() > 0) { |
| 83 | + response.addAccountId(group.getAccountUuid()); |
| 84 | + } |
| 85 | + if (group.getRoleId() > 0) { |
| 86 | + AclRoleResponse roleResp = new AclRoleResponse(); |
| 87 | + roleResp.setId(group.getRoleUuid()); |
| 88 | + roleResp.setName(group.getRoleName()); |
| 89 | + response.addRole(roleResp); |
| 90 | + } |
| 91 | + response.setObjectName("aclgroup"); |
| 92 | + |
| 93 | + |
| 94 | + return response; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public AclGroupResponse setAclGroupResponse(AclGroupResponse response, AclGroupJoinVO group) { |
| 99 | + if (group.getAccountId() > 0) { |
| 100 | + response.addAccountId(group.getAccountUuid()); |
| 101 | + } |
| 102 | + if (group.getRoleId() > 0) { |
| 103 | + AclRoleResponse roleResp = new AclRoleResponse(); |
| 104 | + roleResp.setId(group.getRoleUuid()); |
| 105 | + roleResp.setName(group.getRoleName()); |
| 106 | + response.addRole(roleResp); |
| 107 | + } |
| 108 | + return response; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public List<AclGroupJoinVO> newAclGroupView(AclGroup group) { |
| 113 | + SearchCriteria<AclGroupJoinVO> sc = grpIdSearch.create(); |
| 114 | + sc.setParameters("id", group.getId()); |
| 115 | + return searchIncludingRemoved(sc, null, null, false); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public List<AclGroupJoinVO> searchByIds(Long... grpIds) { |
| 121 | + // set detail batch query size |
| 122 | + int DETAILS_BATCH_SIZE = 2000; |
| 123 | + String batchCfg = _configDao.getValue("detail.batch.query.size"); |
| 124 | + if (batchCfg != null) { |
| 125 | + DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg); |
| 126 | + } |
| 127 | + // query details by batches |
| 128 | + List<AclGroupJoinVO> uvList = new ArrayList<AclGroupJoinVO>(); |
| 129 | + // query details by batches |
| 130 | + int curr_index = 0; |
| 131 | + if (grpIds.length > DETAILS_BATCH_SIZE) { |
| 132 | + while ((curr_index + DETAILS_BATCH_SIZE) <= grpIds.length) { |
| 133 | + Long[] ids = new Long[DETAILS_BATCH_SIZE]; |
| 134 | + for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) { |
| 135 | + ids[k] = grpIds[j]; |
| 136 | + } |
| 137 | + SearchCriteria<AclGroupJoinVO> sc = grpSearch.create(); |
| 138 | + sc.setParameters("idIN", ids); |
| 139 | + List<AclGroupJoinVO> vms = searchIncludingRemoved(sc, null, null, false); |
| 140 | + if (vms != null) { |
| 141 | + uvList.addAll(vms); |
| 142 | + } |
| 143 | + curr_index += DETAILS_BATCH_SIZE; |
| 144 | + } |
| 145 | + } |
| 146 | + if (curr_index < grpIds.length) { |
| 147 | + int batch_size = (grpIds.length - curr_index); |
| 148 | + // set the ids value |
| 149 | + Long[] ids = new Long[batch_size]; |
| 150 | + for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) { |
| 151 | + ids[k] = grpIds[j]; |
| 152 | + } |
| 153 | + SearchCriteria<AclGroupJoinVO> sc = grpSearch.create(); |
| 154 | + sc.setParameters("idIN", ids); |
| 155 | + List<AclGroupJoinVO> vms = searchIncludingRemoved(sc, null, null, false); |
| 156 | + if (vms != null) { |
| 157 | + uvList.addAll(vms); |
| 158 | + } |
| 159 | + } |
| 160 | + return uvList; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public List<AclGroupJoinVO> findAclGroupsByAccount(long accountId) { |
| 165 | + List<AclGroupAccountMapVO> grpMap = _grpAccountDao.listByAccountId(accountId); |
| 166 | + if (grpMap != null && grpMap.size() > 0) { |
| 167 | + Set<Long> grpList = new HashSet<Long>(); |
| 168 | + for (AclGroupAccountMapVO m : grpMap) { |
| 169 | + grpList.add(m.getAclGroupId()); |
| 170 | + } |
| 171 | + return searchByIds(grpList.toArray(new Long[grpList.size()])); |
| 172 | + } |
| 173 | + else{ |
| 174 | + return null; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments