1 /*
2 * File: AbstractDataSet.java
3 * Created: 28.4.2007 11:35:44
4 *
5 * Copyright 2007 Michal Burda.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 package net.sf.webmancer.db;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import net.sf.webmancer.util.ContractChecker;
30
31 /**
32 * @author Michal Burda
33 *
34 */
35 public abstract class AbstractDataSet implements IDataSet, IDatabaseOperationPerformer {
36 /**
37 *
38 */
39 private Map<String, IAttribute> attributes;
40
41 /**
42 *
43 */
44 public AbstractDataSet() {
45 this.attributes = new HashMap<String, IAttribute>();
46 }
47
48 /**
49 * @see net.sf.webmancer.db.IDataSet#createQuery()
50 */
51 public IQuery createQuery() {
52 return new Query(this);
53 }
54
55 /**
56 * @see net.sf.webmancer.db.IDataSet#getAttributes()
57 */
58 public Map<String, IAttribute> getAttributes() {
59 assert attributes != null;
60 return Collections.unmodifiableMap(attributes);
61 }
62
63 /**
64 * @param attribute
65 */
66 public void addAttribute(IAttribute attribute) {
67 ContractChecker.mustNotBeNull(attribute, "attribute");
68 ContractChecker.mustNotBeNull(attribute.getId(), "attribute.getId()");
69 assert attributes != null;
70 this.attributes.put(attribute.getId(), attribute);
71 }
72
73 /**
74 * @param attributes
75 */
76 public void setAttributes(List<IAttribute> attributes) {
77 ContractChecker.mustNotBeNull(attributes, "attributes");
78 for (IAttribute attribute : attributes) {
79 this.addAttribute(attribute);
80 }
81 }
82
83 /**
84 * @see net.sf.webmancer.db.IDatabaseOperationPerformer#performDelete(java.util.List)
85 */
86 public abstract long performDelete(List<ICondition> conditions);
87
88 /**
89 * @see net.sf.webmancer.db.IDatabaseOperationPerformer#performInsert(java.util.Map)
90 */
91 public abstract void performInsert(Map<IAttribute, ICell> row);
92
93 /**
94 * Insert given data into data storage. This method simply calls {@link #performInsert(Map)} for
95 * each row in <code>data</code>. Overriding classes may use some more effective approach
96 * depending on the type of used data storage (e.g. chunked SQL INSERT etc).
97 *
98 * @param data data to be inserted into data storage
99 *
100 * @see net.sf.webmancer.db.IDatabaseOperationPerformer#performInsert(net.sf.webmancer.db.IData)
101 */
102 public void performInsert(IData data) {
103 ContractChecker.mustNotBeNull(data, "data");
104 for (Map<IAttribute, ICell> row : data.getRows()) {
105 performInsert(row);
106 }
107 }
108
109 /**
110 * @see net.sf.webmancer.db.IDatabaseOperationPerformer#performQueryCount(java.util.List)
111 */
112 public abstract long performQueryCount(List<ICondition> conditions);
113
114 /**
115 * @see net.sf.webmancer.db.IDatabaseOperationPerformer#performQueryResult(java.util.Map, java.util.List, long, long, net.sf.webmancer.db.IAttribute, boolean)
116 */
117 public abstract IData performQueryResult(Map<String, IAttribute> attributes, List<ICondition> conditions, long limit, long offset, IAttribute orderingAttribute, boolean ascending);
118
119 /**
120 * @see net.sf.webmancer.db.IDatabaseOperationPerformer#performUpdate(java.util.Map, java.util.List)
121 */
122 public abstract long performUpdate(Map<IAttribute, ICell> row, List<ICondition> conditions);
123
124 }