View Javadoc

1   /* ===================================================================
2    * Copyright 2002-06 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.commandline.defaults;
11  
12  import java.io.IOException;
13  import java.io.PrintWriter;
14  import java.util.Date;
15  import java.util.HashMap;
16  import java.util.Iterator;
17  import java.util.List;
18  
19  import org.apache.log4j.Logger;
20  
21  import com.jcorporate.expresso.core.db.DBException;
22  import com.jcorporate.expresso.core.dbobj.DBObject;
23  import com.jcorporate.expresso.core.security.User;
24  import com.jcorporate.expresso.core.security.UserInfo;
25  import com.jcorporate.expresso.services.dbobj.DefaultUserInfo;
26  import com.jcorporate.expresso.services.dbobj.GroupMembers;
27  import com.jcorporate.expresso.services.dbobj.Setup;
28  import com.jcorporate.expresso.services.dbobj.UserGroup;
29  import com.sri.emo.commandline.NodeTracker;
30  import com.sri.emo.commandline.SqlRowGenerator;
31  import com.sri.emo.commandline.TreeExporter;
32  import com.sri.emo.dbobj.Node;
33  import com.sri.emo.dbobj.selectiontree.TreeSelectionFactory;
34  
35  /***
36   * @author Michael Rimov
37   */
38  public class DefaultTreeExporter extends AbstractNodeExportSupport implements TreeExporter {
39  
40      /***
41       * Log4j logger.
42       */
43      public final Logger log = Logger.getLogger(TreeExporter.class);
44  
45  
46      public DefaultTreeExporter(final NodeTracker nodeTracker, final SqlRowGenerator generator) {
47          super(nodeTracker, generator);
48      }
49  
50  
51      public int exportNodeTree(final PrintWriter writer, final Node baseNode) throws DBException, IOException {
52          int totalCount = 0;
53  
54          totalCount += exportAllUserGroups(writer);
55          totalCount += exportAllSetupValues(writer);
56  
57          //Grabbed from TagSelectionFactory.
58          HashMap map = new HashMap();
59          baseNode.setAttribute(Node.INDENT, new Integer(1));
60          TreeSelectionFactory.getNodesInStronglyRelatedTreeRecursive(baseNode, map);
61          if (log.isDebugEnabled()) {
62              log.debug("Found a total of " + map.size()
63                      + " nodes to export for tree with base node " + baseNode.getNodeId());
64          }
65  
66          for (Iterator i = map.values().iterator(); i.hasNext();) {
67              Node oneNode = (Node) i.next();
68              totalCount += exportNode(writer, oneNode);
69          }
70  
71          return totalCount;
72      }
73  
74      private int exportAllSetupValues(PrintWriter writer) throws DBException {
75          int result = 0;
76          Setup setup = new Setup();
77          List list = setup.searchAndRetrieveList();
78          for (Iterator iterator = list.iterator(); iterator.hasNext();) {
79              Setup aSetup = (Setup) iterator.next();
80  
81              // skip some
82              if ( "MAILFrom".equals(aSetup.getField(Setup.SETUP_CODE))
83                      || "HTTPServ".equals(aSetup.getField(Setup.SETUP_CODE))
84                      || "BugsEmail".equals(aSetup.getField(Setup.SETUP_CODE))
85                      || "AdminEmail".equals(aSetup.getField(Setup.SETUP_CODE))
86                      || "EmailValidateURL".equals(aSetup.getField(Setup.SETUP_CODE))) {
87                  continue;
88              }
89              result += writeDBObject(writer, aSetup);
90          }
91  
92          return result;
93      }
94  
95      /***
96       * @param writer
97       * @return
98       * @throws DBException
99       */
100     private int exportAllUserGroups(final PrintWriter writer) throws DBException {
101         int result = 0;
102 
103         UserGroup grp = new UserGroup();
104         List list = grp.searchAndRetrieveList();
105         for (Iterator iterator = list.iterator(); iterator.hasNext();) {
106             UserGroup group = (UserGroup) iterator.next();
107             result += writeDBObject(writer, group);
108 
109             // make sure our user is member of all groups EXCEPT unknown,
110             if (!UserGroup.NOT_REG_USERS_GROUP.equals(group.getGroupName())
111                     && !UserGroup.UNKNOWN_USERS_GROUP.equals(group.getGroupName())) {
112                 GroupMembers member = new GroupMembers();
113                 member.setGroupName(group.getGroupName());
114                 member.setUID(TESTER_UID);
115                 result += writeDBObject(writer, member);
116             }
117         }
118 
119         // also export the test user
120         User user = new User();
121         UserInfo testerInfo = user.getUserInfo();
122         testerInfo.setUid(TESTER_UID);
123         testerInfo.setLoginName("tester");
124         testerInfo.setEmail("testerForEmo@codeguild.com");
125         testerInfo.setAccountStatus("A");
126         testerInfo.setPassword("tester");
127         testerInfo.setEmailValCode((String) null);
128         testerInfo.setUserName("tester");
129         testerInfo.setRegComplete(true);
130         testerInfo.setRegistrationDomain("default");
131 
132         DBObject info = (DBObject) testerInfo;
133         Date now = new Date();
134         info.setField(DefaultUserInfo.CREATED_DATE, now);
135         info.setField(DefaultUserInfo.UPDATED_DATE, now);
136 
137         result += writeDBObject(writer, info);
138 
139         // add a read-only user
140         testerInfo.setUid(TESTER_UID + 1);
141         testerInfo.setLoginName("reader");
142         testerInfo.setUserName("reader");
143         testerInfo.setEmail("readerPadiExport@codeguild.com");
144         testerInfo.setAccountStatus("A");
145         testerInfo.setPassword("reader");
146 
147         result += writeDBObject(writer, info);
148 
149         // make member of readers group
150         GroupMembers member = new GroupMembers();
151         member.setGroupName("padi_read"); // read group constant is in PADI-specific classes; use literal to prevent dependency
152         member.setUID(TESTER_UID + 1);
153 
154         result += writeDBObject(writer, member);
155 
156         return result;
157     }
158 
159     /***
160      * Exports a node and all of its related attributes, security settings, and relations.
161      *
162      * @param writer
163      * @param node
164      * @return
165      * @throws DBException
166      * @throws IOException
167      */
168     public int exportNode(final PrintWriter writer, final Node node) throws DBException, IOException {
169 
170         int totalCount = 0;
171 
172         //Export the node type.
173         totalCount += exportNodeType(writer, node.getNodeType());
174 
175         //Export the node
176         totalCount += writeDBObject(writer, node);
177 
178         if (totalCount == 0) {
179             if (log.isDebugEnabled()) {
180                 log.debug("Found out that node " + node.getNodeId() + " already exists.  Skipping");
181             }
182             //if we haven't written anything by now, it means
183             //we've visited this node.  Don't try anything else.
184             return totalCount;
185         }
186 
187 //        if (log.isDebugEnabled()) {
188 //            log.debug("Exporting node " + node.getNodeId());
189 //        }
190 
191         //Export all node attributes.
192         totalCount += exportAttributes(writer, node);
193 
194         //Export relations.
195         totalCount += exportRelations(writer, node);
196 
197         totalCount += exportNodeTags(writer, node);
198 
199         return totalCount;
200     }
201 
202 
203 
204 }