View Javadoc
1   /**
2    * Copyright 2014 Internet2
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /**
17   * @author mchyzer
18   * $Id$
19   */
20  package edu.internet2.middleware.grouper.xml.export;
21  
22  import java.io.IOException;
23  import java.io.StringWriter;
24  import java.io.Writer;
25  
26  import org.apache.commons.logging.Log;
27  import org.dom4j.Element;
28  import org.dom4j.ElementHandler;
29  import org.dom4j.ElementPath;
30  import org.hibernate.Query;
31  import org.hibernate.ScrollableResults;
32  import org.hibernate.Session;
33  
34  import com.thoughtworks.xstream.XStream;
35  import com.thoughtworks.xstream.io.HierarchicalStreamReader;
36  import com.thoughtworks.xstream.io.xml.CompactWriter;
37  import com.thoughtworks.xstream.io.xml.Dom4JReader;
38  
39  import edu.internet2.middleware.grouper.Field;
40  import edu.internet2.middleware.grouper.hibernate.AuditControl;
41  import edu.internet2.middleware.grouper.hibernate.GrouperTransactionType;
42  import edu.internet2.middleware.grouper.hibernate.HibUtils;
43  import edu.internet2.middleware.grouper.hibernate.HibernateHandler;
44  import edu.internet2.middleware.grouper.hibernate.HibernateHandlerBean;
45  import edu.internet2.middleware.grouper.hibernate.HibernateSession;
46  import edu.internet2.middleware.grouper.internal.dao.GrouperDAOException;
47  import edu.internet2.middleware.grouper.misc.GrouperVersion;
48  import edu.internet2.middleware.grouper.util.GrouperUtil;
49  import edu.internet2.middleware.grouper.xml.importXml.XmlImportMain;
50  
51  
52  /**
53   *
54   */
55  public class XmlExportField {
56  
57    /**
58     * 
59     */
60    private static final String XML_EXPORT_FIELD_XPATH = "/grouperExport/fields/XmlExportField";
61  
62    /**
63     * 
64     */
65    private static final String FIELDS_XPATH = "/grouperExport/fields";
66  
67    /** uuid */
68    private String uuid;
69    
70    /** name */
71    private String name;
72  
73    /** hibernateVersionNumber */
74    private long hibernateVersionNumber;
75  
76    /** contextId */
77    private String contextId;
78    
79    /**
80     * read privilege
81     */
82    private String readPrivilege;
83    
84    /**
85     * read privilege
86     * @return read privilege
87     */
88    public String getReadPrivilege() {
89      return this.readPrivilege;
90    }
91  
92    /**
93     * read privilege
94     * @param readPrivilege1
95     */
96    public void setReadPrivilege(String readPrivilege1) {
97      this.readPrivilege = readPrivilege1;
98    }
99  
100   /** write privilege */
101   private String writePrivilege;
102 
103   /**
104    * write privilege
105    * @return write privilege
106    */
107   public String getWritePrivilege() {
108     return this.writePrivilege;
109   }
110 
111   /**
112    * write privilege
113    * @param writePrivilege1
114    */
115   public void setWritePrivilege(String writePrivilege1) {
116     this.writePrivilege = writePrivilege1;
117   }
118 
119   /**
120    * type
121    */
122   private String type;
123 
124   /**
125    * logger 
126    */
127   private static final Log LOG = GrouperUtil.getLog(XmlExportField.class);
128   
129   
130   /**
131    * type
132    * @return type
133    */
134   public String getType() {
135     return this.type;
136   }
137 
138   /**
139    * type
140    * @param type1
141    */
142   public void setType(String type1) {
143     this.type = type1;
144   }
145 
146   /**
147    * 
148    */
149   public XmlExportField() {
150     
151   }
152 
153 
154   /**
155    * uuid
156    * @return uuid
157    */
158   public String getUuid() {
159     return this.uuid;
160   }
161 
162   /**
163    * uuid
164    * @param uuid1
165    */
166   public void setUuid(String uuid1) {
167     this.uuid = uuid1;
168   }
169 
170   /**
171    * name
172    * @return name
173    */
174   public String getName() {
175     return this.name;
176   }
177 
178   /**
179    * name
180    * @param name
181    */
182   public void setName(String name) {
183     this.name = name;
184   }
185 
186   /**
187    * hibernateVersionNumber
188    * @return hibernateVersionNumber
189    */
190   public long getHibernateVersionNumber() {
191     return this.hibernateVersionNumber;
192   }
193 
194   /**
195    * hibernateVersionNumber
196    * @param hibernateVersionNumber1
197    */
198   public void setHibernateVersionNumber(long hibernateVersionNumber1) {
199     this.hibernateVersionNumber = hibernateVersionNumber1;
200   }
201 
202   /**
203    * contextId
204    * @return contextId
205    */
206   public String getContextId() {
207     return this.contextId;
208   }
209 
210   /**
211    * contextId
212    * @param contextId1
213    */
214   public void setContextId(String contextId1) {
215     this.contextId = contextId1;
216   }
217   
218   /**
219    * convert to group
220    * @return the group
221    */
222   public Field toField() {
223     Fieldware/grouper/Field.html#Field">Field field = new Field();
224     
225     field.setContextId(this.contextId);
226     field.setHibernateVersionNumber(this.hibernateVersionNumber);
227     field.setName("stemmers".equals(this.name) ? "stemAdmins" : this.name);
228     field.setReadPrivilege("stem".equals(this.readPrivilege) ? "stemAdmin" : this.readPrivilege);
229     field.setTypeString(this.type);
230     field.setUuid(this.uuid);
231     field.setWritePrivilege("stem".equals(this.writePrivilege) ? "stemAdmin" : this.writePrivilege);
232     
233     return field;
234   }
235 
236   /**
237    * @param exportVersion
238    * @return the xml string
239    */
240   public String toXml(GrouperVersion exportVersion) {
241     StringWriter stringWriter = new StringWriter();
242     this.toXml(exportVersion, stringWriter);
243     return stringWriter.toString();
244   }
245 
246   /**
247    * @param exportVersion 
248    * @param writer
249    */
250   public void toXml(
251       @SuppressWarnings("unused") GrouperVersion exportVersion, Writer writer) {
252     XStream xStream = XmlExportUtils.xstream();
253   
254     CompactWriter compactWriter = new CompactWriter(writer);
255     
256     xStream.marshal(this, compactWriter);
257   
258   }
259 
260   /**
261    * parse the xml file for groups
262    * @param xmlImportMain
263    */
264   public static void processXmlSecondPass(final XmlImportMain xmlImportMain) {
265     xmlImportMain.getReader().addHandler( FIELDS_XPATH, 
266         new ElementHandler() {
267             public void onStart(ElementPath path) {
268             }
269             public void onEnd(ElementPath path) {
270                 // process a ROW element
271                 Element row = path.getCurrent();
272   
273                 // prune the tree
274                 row.detach();
275             }
276         }
277     );
278   
279     xmlImportMain.getReader().addHandler( XML_EXPORT_FIELD_XPATH, 
280         new ElementHandler() {
281             public void onStart(ElementPath path) {
282                 // do nothing here...    
283             }
284             public void onEnd(ElementPath path) {
285 
286               Element row = null;
287               try {
288                 // process a ROW element
289                 row = path.getCurrent();
290   
291                 // prune the tree
292                 row.detach();
293   
294                 XmlExportFieldternet2/middleware/grouper/xml/export/XmlExportField.html#XmlExportField">XmlExportField xmlExportFieldFromFile = (XmlExportField)xmlImportMain.getXstream().unmarshal(new Dom4JReader(row));
295                 
296                 Field field = xmlExportFieldFromFile.toField();
297                 
298                 XmlExportUtils.syncImportable(field, xmlImportMain);
299                 
300                 xmlImportMain.incrementCurrentCount();
301               } catch (RuntimeException re) {
302                 LOG.error("Problem importing field: " + XmlExportUtils.toString(row), re);
303                 throw re;
304               }
305             }
306         }
307     );
308   
309   }
310 
311   /**
312    * get db count
313    * @return db count
314    */
315   public static long dbCount() {
316     long result = HibernateSession.byHqlStatic().createQuery("select count(*) from Field").uniqueResult(Long.class);
317     return result;
318   }
319   
320 
321   /**
322    * 
323    * @param writer
324    * @param xmlExportMain
325    */
326   public static void exportFields(final Writer writer, final XmlExportMain xmlExportMain) {
327     //get the members
328     HibernateSession.callbackHibernateSession(GrouperTransactionType.READONLY_OR_USE_EXISTING, AuditControl.WILL_NOT_AUDIT, new HibernateHandler() {
329       
330       public Object callback(HibernateHandlerBean hibernateHandlerBean)
331           throws GrouperDAOException {
332   
333         Session session = hibernateHandlerBean.getHibernateSession().getSession();
334   
335         //select all members in order
336         Query query = session.createQuery(
337             "select theField from Field as theField order by theField.name");
338   
339         GrouperVersionGrouperVersion.html#GrouperVersion">GrouperVersion grouperVersion = new GrouperVersion(GrouperVersion.grouperVersion());
340         try {
341           writer.write("  <fields>\n");
342   
343           //this is an efficient low-memory way to iterate through a resultset
344           ScrollableResults results = null;
345           try {
346             results = query.scroll();
347             while(results.next()) {
348               Object object = results.get(0);
349               final Field="../../../../../../edu/internet2/middleware/grouper/Field.html#Field">Field field = (Field)object;
350               
351               XmlExportField xmlExportField = field.xmlToExportField(grouperVersion);
352               writer.write("    ");
353               xmlExportField.toXml(grouperVersion, writer);
354               writer.write("\n");
355               xmlExportMain.incrementRecordCount();
356             }
357           } finally {
358             HibUtils.closeQuietly(results);
359           }
360           
361           if (xmlExportMain.isIncludeComments()) {
362             writer.write("\n");
363           }
364           
365           //end the fields element 
366           writer.write("  </fields>\n");
367         } catch (IOException ioe) {
368           throw new RuntimeException("Problem with streaming fields", ioe);
369         }
370         return null;
371       }
372     });
373   }
374 
375   /**
376    * take a reader (e.g. dom reader) and convert to an xml export field
377    * @param exportVersion
378    * @param hierarchicalStreamReader
379    * @return the bean
380    */
381   public static XmlExportField fromXml(@SuppressWarnings("unused") GrouperVersion exportVersion, 
382       HierarchicalStreamReader hierarchicalStreamReader) {
383     XStream xStream = XmlExportUtils.xstream();
384     
385     XmlExportField./edu/internet2/middleware/grouper/xml/export/XmlExportField.html#XmlExportField">XmlExportField xmlExportField = (XmlExportField)xStream.unmarshal(hierarchicalStreamReader);
386   
387     return xmlExportField;
388   }
389 
390   /**
391    * 
392    * @param exportVersion
393    * @param xml
394    * @return the object from xml
395    */
396   public static XmlExportField fromXml(
397       @SuppressWarnings("unused") GrouperVersion exportVersion, String xml) {
398     XStream xStream = XmlExportUtils.xstream();
399     
400     XmlExportField./edu/internet2/middleware/grouper/xml/export/XmlExportField.html#XmlExportField">XmlExportField xmlExportField = (XmlExportField)xStream.fromXML(xml);
401   
402     return xmlExportField;
403   }
404 
405   /**
406    * parse the xml file for fields
407    * @param xmlImportMain
408    */
409   public static void processXmlFirstPass(final XmlImportMain xmlImportMain) {
410     xmlImportMain.getReader().addHandler( FIELDS_XPATH, 
411         new ElementHandler() {
412             public void onStart(ElementPath path) {
413             }
414             public void onEnd(ElementPath path) {
415                 // process a ROW element
416                 Element row = path.getCurrent();
417 
418                 // prune the tree
419                 row.detach();
420             }
421         }
422     );
423 
424     xmlImportMain.getReader().addHandler( XML_EXPORT_FIELD_XPATH, 
425         new ElementHandler() {
426             public void onStart(ElementPath path) {
427                 // do nothing here...    
428             }
429             public void onEnd(ElementPath path) {
430                 // process a ROW element
431                 Element row = path.getCurrent();
432 
433                 // prune the tree
434                 row.detach();
435 
436                 xmlImportMain.incrementTotalImportFileCount();
437             }
438         }
439     );
440  
441   }
442 
443 }