[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/importers ...
Christian Bauer
christian at hibernate.org
Thu Aug 30 12:51:00 EDT 2007
User: cbauer
Date: 07/08/30 12:51:00
Modified: examples/wiki/src/main/org/jboss/seam/wiki/core/importers
ZipImporter.java
Log:
Finished Blosxom importer (not perfect though) and minor bugfixes
Revision Changes Path
1.3 +62 -43 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/importers/ZipImporter.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ZipImporter.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/importers/ZipImporter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- ZipImporter.java 29 Aug 2007 00:29:22 -0000 1.2
+++ ZipImporter.java 30 Aug 2007 16:51:00 -0000 1.3
@@ -21,11 +21,7 @@
import javax.swing.*;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-import java.util.Date;
-import java.util.Map;
-import java.util.SortedMap;
-import java.util.TreeMap;
+import java.util.*;
import java.io.*;
import net.sf.jmimemagic.Magic;
@@ -50,7 +46,7 @@
public boolean handleImport(EntityManager em, File zipFile) {
if (zipFile.getData().length == 0) return true;
- SortedMap<Date, Node> newNodes = new TreeMap<Date, Node>();
+ Map<String, Object> newObjects = new HashMap<String, Object>();
ByteArrayInputStream byteStream = null;
ZipInputStream zipInputStream = null;
@@ -79,9 +75,9 @@
baos.close();
uncompressedBytes = baos.toByteArray();
- Node newNode = createNewNode(em, zipFile, ze, uncompressedBytes);
- if (newNode != null) {
- newNodes.put(newNode.getCreatedOn(), newNode);
+ Object newObject = createNewObject(em, zipFile, ze, uncompressedBytes);
+ if (newObject != null) {
+ newObjects.put(ze.getName(), newObject);
}
zipInputStream.closeEntry();
@@ -98,42 +94,19 @@
}
}
- int i = 0;
- for (Map.Entry<Date, Node> entry : newNodes.entrySet()) {
- log.debug("validating new node");
-
- ClassValidator validator = Validators.instance().getValidator(entry.getValue().getClass());
- InvalidValue[] invalidValues = validator.getInvalidValues(entry.getValue());
- if (invalidValues != null && invalidValues.length > 0) {
- log.debug("new node is invalid: " + entry.getValue());
- for (InvalidValue invalidValue : invalidValues) {
- getFacesMessages().addFromResourceBundleOrDefault(
- FacesMessage.SEVERITY_ERROR,
- "invalidImportedDocumentName",
- "Skipping entry '" + entry.getValue().getName() + "', invalid: " + invalidValue.getMessage()
- );
+ // By default append them to the
+ persistNewNodesSorted(
+ em,
+ zipFile,
+ newObjects,
+ new Comparator() {
+ public int compare(Object o, Object o1) {
+ if ( !(o instanceof Node) && !(o1 instanceof Node) ) return 0;
+ return ((Node)o).getWikiname().compareTo( ((Node)o1).getWikiname() );
}
- continue;
}
-
- log.debug("persisting newly imported node: " + entry.getValue());
- zipFile.getParent().addChild(entry.getValue());
- em.persist(entry.getValue());
- getFacesMessages().addFromResourceBundleOrDefault(
- FacesMessage.SEVERITY_INFO,
- "importedFile",
- "Created file '" + entry.getValue().getName() + "' in current directory"
);
- // Batch the work (we can't clear because of nested set updates, unfortunately)
- i++;
- if (i==100) {
- em.flush();
- i = 0;
- }
-
- }
-
return true;
}
@@ -167,7 +140,7 @@
}
}
- protected Node createNewNode(EntityManager em, File zipFile, ZipEntry zipEntry, byte[] uncompressedBytes) {
+ protected Object createNewObject(EntityManager em, File zipFile, ZipEntry zipEntry, byte[] uncompressedBytes) {
log.debug("creating new file from zip entry: " + zipEntry.getName());
File wikiFile = new File();
@@ -210,5 +183,51 @@
return wikiFile;
}
+ protected void persistNewNodesSorted(EntityManager em, File zipFile, Map<String, Object> newObjects, Comparator comparator) {
+
+ List<Node> newNodes = new ArrayList<Node>();
+ for (Object newObject : newObjects.values()) {
+ if (newObject instanceof Node) {
+ newNodes.add((Node)newObject);
+ }
+ }
+ Collections.sort(newNodes, comparator);
+
+ int i = 0;
+ for (Node newNode : newNodes) {
+ log.debug("validating new node");
+
+ ClassValidator validator = Validators.instance().getValidator(newNode.getClass());
+ InvalidValue[] invalidValues = validator.getInvalidValues(newNode);
+ if (invalidValues != null && invalidValues.length > 0) {
+ log.debug("new node is invalid: " + newNode);
+ for (InvalidValue invalidValue : invalidValues) {
+ getFacesMessages().addFromResourceBundleOrDefault(
+ FacesMessage.SEVERITY_ERROR,
+ "invalidImportedDocumentName",
+ "Skipping entry '" + newNode.getName() + "', invalid: " + invalidValue.getMessage()
+ );
+ }
+ continue;
+ }
+
+ log.debug("persisting newly imported node: " + newNode);
+ zipFile.getParent().addChild(newNode);
+ em.persist(newNode);
+ getFacesMessages().addFromResourceBundleOrDefault(
+ FacesMessage.SEVERITY_INFO,
+ "importedFile",
+ "Created file '" + newNode.getName() + "' in current directory"
+ );
+
+ // Batch the work (we can't clear because of nested set updates, unfortunately)
+ i++;
+ if (i==100) {
+ em.flush();
+ i = 0;
+ }
+ }
+ }
+
}
More information about the jboss-cvs-commits
mailing list