1
2
3
4
5
6
7
8
9
10
11 package com.sri.emo.commandline.defaults;
12
13 import java.io.PrintWriter;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import org.apache.log4j.Logger;
19
20 import com.jcorporate.expresso.core.db.DBException;
21 import com.jcorporate.expresso.core.dbobj.DBObject;
22 import com.jcorporate.expresso.core.dbobj.RowSecuredDBObject;
23 import com.jcorporate.expresso.core.security.SuperUser;
24 import com.jcorporate.expresso.core.security.User;
25 import com.jcorporate.expresso.services.dbobj.DBObjSecurity;
26 import com.jcorporate.expresso.services.dbobj.RowGroupPerms;
27 import com.jcorporate.expresso.services.dbobj.RowPermissions;
28 import com.sri.emo.commandline.NodeTracker;
29 import com.sri.emo.commandline.SqlRowGenerator;
30
31 abstract public class AbstractExportSupport {
32
33 private final Logger log = Logger.getLogger(AbstractExportSupport.class);
34
35 /***
36 * The expected UID of the tester.
37 */
38 protected static final int TESTER_UID = 4;
39 /***
40 * Tracks the nodes.
41 */
42 private final NodeTracker nodeTracker;
43 /***
44 * Generates the insertion sql.
45 */
46 private final SqlRowGenerator generator;
47
48 public AbstractExportSupport(final NodeTracker nodeTracker, final SqlRowGenerator generator) {
49 super();
50 this.nodeTracker = nodeTracker;
51 this.generator = generator;
52 }
53
54 /***
55 * Exports a dbobject. This function checks to see if a particular dbobject has been already
56 * export before performing the export.
57 *
58 * @param writer the output stream to write the SQL insert to.
59 * @param dbobject the dbobject we're exporting.
60 * @return number of INSERT statements written; 0 if the object has already been visited.
61 * @throws DBException
62 */
63 protected int writeDBObject(final PrintWriter writer, final DBObject dbobject) throws DBException {
64 if (dbobject == null) {
65 return 0;
66 }
67
68 List sqlStatements = getSQL(dbobject);
69 writeSql(writer, sqlStatements);
70 return sqlStatements.size();
71 }
72
73
74 /***
75 * Writes a list of sql statements to the printwriter.
76 * @param writer the printwriter to append the data to.
77 * @param listOfSql list of strings.
78 * @throws DBException if the data is not appropriately a sql string.
79 */
80 protected void writeSql(final PrintWriter writer, final List listOfSql) throws DBException {
81 for (Iterator iterator = listOfSql.iterator(); iterator.hasNext();) {
82 Object obj = iterator.next();
83
84 if (!(obj instanceof String)) {
85 String s = obj.toString();
86 log.warn("Not a string: " + obj.getClass().getName() + " has toString: " + s);
87 if (!s.startsWith("INSERT")) {
88 throw new DBException("Cannot use object returned by getSQL for "
89 +"SQL insert; see previous error lines");
90 }
91 }
92
93 writer.println(obj.toString());
94 }
95
96 }
97
98 /***
99 * Writes a list of dbobjects to the output stream.
100 * @param writer
101 * @param severalDBObjects
102 * @return
103 * @throws DBException
104 */
105 protected int writeDBObject(final PrintWriter writer, final List severalDBObjects) throws DBException {
106 int recordCount = 0;
107
108 for (Iterator i = severalDBObjects.iterator(); i.hasNext();) {
109 DBObject eachObject = (DBObject)i.next();
110 recordCount += writeDBObject(writer,eachObject);
111 }
112
113 return recordCount;
114 }
115
116
117 protected List exportSecuredDBobjectEntries(final DBObject targetObject) throws DBException {
118 List result = new ArrayList();
119
120
121 DBObjSecurity dbobjSecurity = new DBObjSecurity(SuperUser.INSTANCE);
122 dbobjSecurity.set(DBObjSecurity.DBOBJECT_NAME, targetObject.getClass().getName());
123 List allSecurityEntries = dbobjSecurity.searchAndRetrieveList(DBObjSecurity.GROUP_NAME);
124
125
126 User tester = User.getUserFromId(TESTER_UID);
127 List groups = tester.getGroupsList();
128
129
130
131 for (Iterator i = groups.iterator(); i.hasNext();) {
132 String groupName = (String) i.next();
133 result.addAll(getEntriesForUserGroupName(groupName, allSecurityEntries));
134 }
135
136 return result;
137 }
138
139 private List getEntriesForUserGroupName(final String groupName, final List allEntries) throws DBException {
140 List result = new ArrayList();
141 for (Iterator i = allEntries.iterator(); i.hasNext();) {
142 DBObjSecurity eachEntry = (DBObjSecurity)i.next();
143
144 if (groupName.equals(eachEntry.getField(DBObjSecurity.GROUP_NAME))) {
145 if (nodeTracker.isObjectVisited(eachEntry)) {
146 continue;
147 }
148 nodeTracker.markObjectAsVisited(eachEntry);
149 result.add(generator.generateInsert(eachEntry));
150 }
151
152 }
153 return result;
154 }
155
156 /***
157 * Creates default Row security for each row secured object being export.
158 *
159 * @param object
160 * @return the number of rows added to the export table.
161 * @throws DBException
162 */
163 protected List exportSecurityEntries(final RowSecuredDBObject object) throws DBException {
164
165 List list = new ArrayList();
166
167 RowPermissions rowPermissions = object.getPermissions();
168 rowPermissions.owner(AbstractExportSupport.TESTER_UID);
169 if (log.isDebugEnabled()) {
170 log.debug("Generating row permission sql " + rowPermissions.getKey());
171 }
172
173 list.addAll(getSQL(rowPermissions));
174
175
176 List grps = object.getGroups();
177 for (Iterator iterator = grps.iterator(); iterator.hasNext();) {
178 RowGroupPerms grpperms = (RowGroupPerms) iterator.next();
179
180
181 if (log.isDebugEnabled()) {
182 log.debug("Exporting row group perms " + grpperms.getKey());
183 }
184 list.addAll(getSQL(grpperms));
185 }
186
187 return list;
188 }
189
190 /***
191 * generate list of INSERT statements for given object (and any security objects
192 * associated with the target object), IFF that object hasn't already been visited.
193 *
194 * @return list of strings: all the SQL insert statements for the given object
195 */
196 public List getSQL(DBObject dbobject) throws DBException {
197 List list = new ArrayList();
198 if (nodeTracker.isObjectVisited(dbobject)) {
199 if (log.isInfoEnabled()) {
200 log.info("Skipping sql generation for object " + dbobject.toString() + " with key "
201 + dbobject.getKey() + " it already appears to have been exported");
202 }
203 } else {
204
205 nodeTracker.markObjectAsVisited(dbobject);
206
207 String toSave = generator.generateInsert(dbobject);
208
209 if (log.isDebugEnabled()) {
210 log.debug("Created sql insert statement: \t " + toSave);
211 }
212
213 list.add(toSave);
214
215 if (dbobject instanceof RowSecuredDBObject) {
216 if (log.isDebugEnabled()) {
217 log.debug("Exporting row security entries for dbobject." + dbobject);
218 }
219
220 list.addAll(exportSecurityEntries((RowSecuredDBObject) dbobject));
221 }
222
223 list.addAll(exportSecuredDBobjectEntries(dbobject));
224 }
225 return list;
226 }
227
228 /***
229 * Retrieve the SQL generator.
230 * @return generator or null
231 */
232 protected SqlRowGenerator getGenerator() {
233 return generator;
234 }
235
236 /***
237 * Retrieve the node tracker.
238 * @return tracker or null
239 */
240 protected NodeTracker getNodeTracker() {
241 return nodeTracker;
242 }
243
244
245 }