View Javadoc

1   package com.sri.emo.annotations;
2   
3   import com.jcorporate.expresso.core.controller.ControllerRequest;
4   import com.jcorporate.expresso.core.dataobjects.DataException;
5   import com.jcorporate.expresso.core.db.DBConnection;
6   import com.jcorporate.expresso.core.db.DBException;
7   import com.jcorporate.expresso.core.dbobj.DBField;
8   import com.jcorporate.expresso.core.dbobj.MultiDBObject;
9   import com.jcorporate.expresso.core.dbobj.SecurDBObject;
10  import com.jcorporate.expresso.core.security.ReadOnlyUser;
11  import com.sri.emo.dbobj.Node;
12  import java.util.ArrayList;
13  import java.util.Date;
14  import java.util.Iterator;
15  import java.util.List;
16  
17  /***
18   * Table of tags associated with the given nodes.
19   *
20   * @author Michael Rimov
21   */
22  public class NodeTag extends SecurDBObject {
23  
24      /***
25  	 * 
26  	 */
27  	private static final long serialVersionUID = 4270471014662888765L;
28  
29  	public static final String TAG_ID = "Id";
30  
31      public static final String TAG_PARENT_NODE = "NodeId";
32  
33      public static final String TAG_VALUE = "TagValue";
34  
35      public static final String TAG_ADDED_BY = "AddedByUser";
36  
37      public static final String TAG_ADDED_ON = "AddedOn";
38      public static final String TAGS_TABLE = "tags";
39  
40  
41      public NodeTag() throws DBException {
42      }
43  
44      public NodeTag(final DBConnection newConnection) throws DBException {
45          super(newConnection);
46      }
47  
48      public NodeTag(final DBConnection newConnection, final String setupTablesContext) throws DBException {
49          super(newConnection, setupTablesContext);
50      }
51  
52      public NodeTag(final ControllerRequest request) throws DBException {
53          super(request);
54      }
55  
56      public NodeTag(final ReadOnlyUser readOnlyUser) throws DBException {
57          super(readOnlyUser);
58      }
59  
60      protected void setupFields() throws DBException {
61          setTargetTable(TAGS_TABLE);
62          setDescription("Node Tags");
63  
64          addField(TAG_ID, DBField.AUTOINC_TYPE, 0, false, "Tag ID");
65          addField(TAG_PARENT_NODE, DBField.INTEGER_TYPE, 0, false, "Parent Node ID");
66          addField(TAG_VALUE, DBField.VARCHAR_TYPE, 128, false, "Tag Value");
67          addField(TAG_ADDED_BY, DBField.VARCHAR_TYPE, 128, false, "Added By");
68          addField(TAG_ADDED_ON, DBField.DATE_TYPE, 0, false, "Added On");
69  
70          setReadOnly(TAG_ADDED_BY);
71          setReadOnly(TAG_ADDED_ON);
72          addKey(TAG_ID);
73  
74          //Index on the parent node and the date added to increase load speeds.
75          addIndex("node_tag_date_idx", TAG_PARENT_NODE + "," + TAG_ADDED_ON, false);
76          addIndex("node_tag_idx", TAG_PARENT_NODE, false);
77      }
78  
79  
80      /***
81       * Sets the value of the tag.
82       *
83       * @param tagValue String may be initially null, but may not be null once
84       *                 added or updated.
85       * @throws DataException
86       */
87      public void setTagValue(final String tagValue) throws DataException {
88          this.set(TAG_VALUE, tagValue);
89      }
90  
91      /***
92       * Sets the parent node id.
93       *
94       * @param nodeId Integer
95       * @throws DataException
96       */
97      public void setNodeId(final Integer nodeId) throws DataException {
98          this.set(TAG_PARENT_NODE, nodeId);
99      }
100 
101     public void setNodeId(final String nodeId) throws DataException {
102         this.set(TAG_PARENT_NODE, nodeId);
103     }
104 
105     /***
106      * Sets the current user for the 'added by' node value.
107      *
108      * @throws DBException
109      */
110     public void setAddedBy() throws DBException {
111         setAddedBy(this.getRequestingUser().getLoginName());
112     }
113 
114     /***
115      * Set who it was added by.
116      *
117      * @param addedBy String
118      * @throws DataException
119      */
120     public void setAddedBy(final String addedBy) throws DataException {
121         this.set(TAG_ADDED_BY, addedBy);
122     }
123 
124     /***
125      * Sets the current date for the item.
126      *
127      * @throws DataException
128      */
129     public void setAddedOn() throws DataException {
130         setAddedOn(new Date());
131     }
132 
133     public void setAddedOn(final Date addedOn) throws DataException {
134         this.set(TAG_ADDED_ON, addedOn);
135     }
136 
137     public Integer getTagId() throws DBException {
138         return (Integer) this.getDataField(TAG_ID).asInteger();
139     }
140 
141     public String getTagValue() throws DBException {
142         return this.getDataField(TAG_VALUE).asString();
143     }
144 
145     public Integer getNodeId() throws DBException {
146         return this.getDataField(TAG_PARENT_NODE).asInteger();
147     }
148 
149     public String getAddedBy() throws DBException {
150         return this.getDataField(TAG_ADDED_BY).asString();
151     }
152 
153     public Date getAddedOn() throws DBException {
154         return getDataField(TAG_ADDED_ON).asDate();
155     }
156 
157     public void add(String userId) throws DBException {
158         this.setAddedBy(userId);
159         this.setAddedOn();
160         super.add();
161     }
162 
163     /***
164      * @return array of nodes based on this tag, using value and 'addedby' IFF those
165      */
166     public Node[] getTaggedNodes() throws DBException {
167 
168         MultiDBObject joinObject = new MultiDBObject();
169         joinObject.setDataContext(getDataContext());
170 
171         // we join a tag
172         joinObject.addDBObj(getClass().getName(), TAGS_TABLE);
173 
174         // and a node
175         joinObject.addDBObj(Node.class.getName(), Node.NODE_TABLE);
176         
177 
178         // and make sure the relation is for this node as source
179         joinObject.setForeignKey(TAGS_TABLE, TAG_PARENT_NODE, Node.NODE_TABLE, Node.NODE_ID);
180 
181         boolean hasCriteria = false;
182         if (getTagValue().length() > 0) {
183             joinObject.setField(TAGS_TABLE, TAG_VALUE, getTagValue());
184             hasCriteria = true;
185         }
186 
187         if (getAddedBy() != null && getAddedBy().length() > 0) {
188             joinObject.setField(TAGS_TABLE, TAG_ADDED_BY, getAddedBy());
189             hasCriteria = true;
190         }
191 
192         if ( !hasCriteria) return new Node[0];
193 
194         // sort by node title
195         List list = joinObject.searchAndRetrieveList(Node.NODE_TITLE);  //?Relation.RELATION_ORDER
196 
197         ArrayList resultList = new ArrayList();
198 
199         for (Iterator iterator = list.iterator(); iterator.hasNext();) {
200             MultiDBObject aMultiObject = (MultiDBObject) iterator.next();
201             Node node = (Node) aMultiObject.getDBObject(Node.NODE_TABLE);
202             resultList.add(node);
203         }
204 
205 
206         return (Node[]) resultList.toArray(new Node[resultList.size()]);
207 
208     }
209 }