|
| 1 | +/** |
| 2 | + * Copyright (C) 2011 Cloud.com, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This software is licensed under the GNU General Public License v3 or later. |
| 5 | + * |
| 6 | + * It is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or any later version. |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | +package com.cloud.dc.dao; |
| 19 | + |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import javax.ejb.Local; |
| 25 | + |
| 26 | +import com.cloud.dc.DcDetailVO; |
| 27 | +import com.cloud.utils.db.GenericDaoBase; |
| 28 | +import com.cloud.utils.db.SearchBuilder; |
| 29 | +import com.cloud.utils.db.SearchCriteria; |
| 30 | +import com.cloud.utils.db.Transaction; |
| 31 | + |
| 32 | +@Local(value=DcDetailsDao.class) |
| 33 | +public class DcDetailsDaoImpl extends GenericDaoBase<DcDetailVO, Long> implements DcDetailsDao { |
| 34 | + protected final SearchBuilder<DcDetailVO> DcSearch; |
| 35 | + protected final SearchBuilder<DcDetailVO> DetailSearch; |
| 36 | + |
| 37 | + protected DcDetailsDaoImpl() { |
| 38 | + DcSearch = createSearchBuilder(); |
| 39 | + DcSearch.and("dcId", DcSearch.entity().getDcId(), SearchCriteria.Op.EQ); |
| 40 | + DcSearch.done(); |
| 41 | + |
| 42 | + DetailSearch = createSearchBuilder(); |
| 43 | + DetailSearch.and("dcId", DetailSearch.entity().getDcId(), SearchCriteria.Op.EQ); |
| 44 | + DetailSearch.and("name", DetailSearch.entity().getName(), SearchCriteria.Op.EQ); |
| 45 | + DetailSearch.done(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public DcDetailVO findDetail(long dcId, String name) { |
| 50 | + SearchCriteria<DcDetailVO> sc = DetailSearch.create(); |
| 51 | + sc.setParameters("dcId", dcId); |
| 52 | + sc.setParameters("name", name); |
| 53 | + |
| 54 | + return findOneIncludingRemovedBy(sc); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Map<String, String> findDetails(long dcId) { |
| 59 | + SearchCriteria<DcDetailVO> sc = DcSearch.create(); |
| 60 | + sc.setParameters("dcId", dcId); |
| 61 | + |
| 62 | + List<DcDetailVO> results = search(sc, null); |
| 63 | + Map<String, String> details = new HashMap<String, String>(results.size()); |
| 64 | + for (DcDetailVO result : results) { |
| 65 | + details.put(result.getName(), result.getValue()); |
| 66 | + } |
| 67 | + return details; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void deleteDetails(long dcId) { |
| 72 | + SearchCriteria sc = DcSearch.create(); |
| 73 | + sc.setParameters("dcId", dcId); |
| 74 | + |
| 75 | + List<DcDetailVO> results = search(sc, null); |
| 76 | + for (DcDetailVO result : results) { |
| 77 | + remove(result.getId()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void persist(long dcId, Map<String, String> details) { |
| 83 | + Transaction txn = Transaction.currentTxn(); |
| 84 | + txn.start(); |
| 85 | + SearchCriteria<DcDetailVO> sc = DcSearch.create(); |
| 86 | + sc.setParameters("dcId", dcId); |
| 87 | + expunge(sc); |
| 88 | + |
| 89 | + for (Map.Entry<String, String> detail : details.entrySet()) { |
| 90 | + DcDetailVO vo = new DcDetailVO(dcId, detail.getKey(), detail.getValue()); |
| 91 | + persist(vo); |
| 92 | + } |
| 93 | + txn.commit(); |
| 94 | + } |
| 95 | +} |
0 commit comments