1
2
3
4
5
6
7
8
9
10
11 package com.sri.emo.commandline.defaults;
12
13 import java.io.IOException;
14 import java.io.PrintWriter;
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.db.exception.DBRecordNotFoundException;
22 import com.jcorporate.expresso.core.dbobj.DBObject;
23 import com.jcorporate.expresso.core.security.SuperUser;
24 import com.sri.emo.annotations.NodeTag;
25 import com.sri.emo.commandline.NodeTracker;
26 import com.sri.emo.commandline.SqlRowGenerator;
27 import com.sri.emo.dbobj.Attribute;
28 import com.sri.emo.dbobj.IPartHandler;
29 import com.sri.emo.dbobj.Node;
30 import com.sri.emo.dbobj.NodeType;
31 import com.sri.emo.dbobj.Part;
32 import com.sri.emo.dbobj.PickList;
33 import com.sri.emo.dbobj.Relation;
34 import com.sri.emo.dbobj.RelationType;
35
36 /***
37 * Contains functions that
38 * @author Michael Rimov
39 */
40 public abstract class AbstractNodeExportSupport extends AbstractExportSupport {
41
42 private Logger log = Logger.getLogger(AbstractNodeExportSupport.class);
43
44 public AbstractNodeExportSupport(NodeTracker nodeTracker,
45 SqlRowGenerator generator) {
46 super(nodeTracker, generator);
47 }
48
49 protected int exportNodeTags(final PrintWriter writer, final Node node) throws DBException {
50 int total = 0;
51 List tags = node.getTags();
52 for (Iterator i = tags.iterator(); i.hasNext();) {
53 NodeTag eachTag = (NodeTag) i.next();
54 total += writeDBObject(writer, eachTag);
55 }
56 return total;
57 }
58
59 protected int exportNodeType(final PrintWriter writer, final String nodeEntityName) throws DBException {
60 NodeType nt = new NodeType();
61
62 nt.setEntityName(nodeEntityName);
63
64 if (!nt.find()) {
65 throw new DBException("Could not find node type " + nodeEntityName);
66 }
67
68 int total = writeDBObject(writer, nt);
69
70 if (total == 0) {
71 return 0;
72 }
73
74 if (log.isDebugEnabled()) {
75 log.debug("Exporting node type " + nt.getId());
76 }
77 total += exportParts(writer, nt);
78 total += exportPicklists(writer, nt);
79
80
81 if (log.isDebugEnabled()) {
82 log.debug("Finished Exporting Node Type and Related Nodes");
83 }
84
85 return total;
86 }
87
88 /***
89 * @param writer
90 * @param nt
91 * @return the number of objects added to the export script.
92 * @throws DBException
93 */
94 protected int exportParts(final PrintWriter writer, final NodeType nt) throws DBException {
95 int total = 0;
96 Part[] parts = nt.getParts();
97 for (int i = 0; i < parts.length; i++) {
98 Part aPart = parts[i];
99 total += writeDBObject(writer, aPart);
100
101
102 RelationType reltype = aPart.getNodeRelationEntity();
103 if (reltype != null) {
104 int before = total;
105 total += writeDBObject(writer, reltype);
106 if (total > before && log.isDebugEnabled()) {
107 log.debug("Exported relation type: " + reltype.getKey());
108 }
109 }
110 }
111
112 return total;
113 }
114
115 protected int exportPicklists(final PrintWriter writer, final NodeType nt) throws DBException {
116 PickList pl = new PickList();
117 pl.setNodeType(nt.getEntityName());
118 List results = pl.searchAndRetrieveList();
119 int total = 0;
120 for (Iterator i = results.iterator(); i.hasNext();) {
121 PickList eachPickList = (PickList) i.next();
122 if (log.isDebugEnabled()) {
123 log.debug("Exporting picklist " + eachPickList.getID());
124 }
125
126 total += writeDBObject(writer, eachPickList);
127 }
128 return total;
129 }
130
131 public int exportAttributes(final PrintWriter writer, final Node owner) throws DBException, IOException {
132 int totalCount = 0;
133 List allAttributes = owner.getAttributes();
134 for (Iterator i = allAttributes.iterator(); i.hasNext();) {
135 Attribute eachAttribute = (Attribute) i.next();
136 totalCount += writeDBObject(writer, eachAttribute);
137
138 Part eachPart = eachAttribute.getPart();
139 if (eachPart == null) {
140 log.error("Got null part for attribute id "
141 + eachAttribute.getAttribId() + " on node id "
142 + owner.getNodeId());
143 } else {
144 IPartHandler handler = eachPart.getCustomHandler();
145 if (handler != null) {
146 String[] insertStatements = handler.getInsertStatements(this, (DBObject)eachAttribute);
147 for (int insertIndex = 0; insertIndex < insertStatements.length; insertIndex++) {
148 writer.println(insertStatements[insertIndex]);
149 }
150
151 totalCount += insertStatements.length;
152 }
153 }
154 }
155
156 return totalCount;
157 }
158
159 /***
160 * This function exports the relations where the parameter owner is the SOURCE. Currently, we
161 * cannot export nodes where parameter owner is dest because the other end of that relation might
162 * not be exported, at which point the GUI will blow up when the node is brought up to edit.
163 *
164 * @param writer
165 * @param owner
166 * @return the number of records exported.
167 * @throws DBException
168 * @throws IOException
169 */
170 public int exportRelations(final PrintWriter writer, final Node owner) throws DBException, IOException {
171 int totalCount = 0;
172
173 Relation relation = new Relation(SuperUser.INSTANCE);
174 relation.setSrcId(owner.getNodeId());
175 List relations = relation.searchAndRetrieveList();
176
177 for (Iterator i = relations.iterator(); i.hasNext();) {
178 Relation eachRelation = (Relation) i.next();
179 RelationType relationType = eachRelation.getType();
180 if (eachRelation.getDestId() == null || eachRelation.getDestId().length() == 0) {
181 log.warn("Invalid data for node relations. Src id "
182 + owner.getNodeId() + " and relation type "
183 + relationType.getRelTypeDisplayName()
184 + " there was no destination node id.\n\t Skipping record.");
185 continue;
186 }
187
188
189 totalCount += writeDBObject(writer, relationType);
190
191
192
193 if (eachRelation.isStrong()) {
194
195
196 totalCount += writeDBObject(writer, eachRelation);
197
198
199 if (relationType.isReflexive()) {
200 Relation inverse = new Relation(SuperUser.INSTANCE);
201 inverse.setDestId(owner.getNodeId());
202 inverse.setSrcId(eachRelation.getDestId());
203 inverse.setRelationTypeName(relationType.getInverseRel());
204 try {
205 inverse.retrieve();
206 totalCount += writeDBObject(writer, inverse);
207 } catch (DBRecordNotFoundException ex) {
208 log.warn("Could not find an inverse relation to node " + owner.getNodeId()
209 + " using inverse relation type "
210 + relationType.getInverseRel());
211 }
212 }
213 }
214 }
215
216 return totalCount;
217 }
218
219 }