/* (non-Javadoc)
   * @see org.hibernate.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
   */
  public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
      throws HibernateException, SQLException {
    byte[] bytes = rs.getBytes(names[0]);
    if (rs.wasNull()) {
      return null;
    }

    // TODO figure out how to inject this
    HomeFactory homeFactory = (HomeFactory) ComponentManager.getInstance().get("xmlHomeFactory");
    WritableObjectHome home = (WritableObjectHome) homeFactory.getHome("agent");

    StructuredArtifact artifact = (StructuredArtifact) home.createInstance();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    SAXBuilder saxBuilder = new SAXBuilder();
    saxBuilder.setFeature(
        "http://apache.org/xml/features/disallow-doctype-decl", true); // SAK-23245
    try {
      Document doc = saxBuilder.build(in);
      artifact.setBaseElement(doc.getRootElement());
    } catch (JDOMException e) {
      throw new HibernateException(e);
    } catch (IOException e) {
      throw new HibernateException(e);
    }
    return artifact;
  }
 public boolean equals(Object other) {
   if (other == null || !(other instanceof StructuredArtifact)) {
     return false;
   }
   StructuredArtifact in = (StructuredArtifact) other;
   return getId().equals(in.getId());
 }
 /* (non-Javadoc)
  * @see org.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
  */
 public void nullSafeSet(PreparedStatement st, Object value, int index)
     throws HibernateException, SQLException {
   if (value == null) {
     st.setNull(index, Types.VARBINARY);
   } else {
     StructuredArtifact artifact = (StructuredArtifact) value;
     Document doc = new Document();
     Element rootElement = artifact.getBaseElement();
     rootElement.detach();
     doc.setRootElement(rootElement);
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     XMLOutputter xmlOutputter = new XMLOutputter();
     try {
       xmlOutputter.output(doc, out);
     } catch (IOException e) {
       throw new HibernateException(e);
     }
     st.setBytes(index, out.toByteArray());
   }
 }