Author: deepal
Date: Mon Feb 25 00:44:40 2008
New Revision: 14151

Log:

get the import and export working correctly , say we get a check out and do 
some changes to registry (delete a file), then if we get a new chechout then 
those delete will be reflected into the local file system

Modified:
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryClientUtils.java

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryClientUtils.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryClientUtils.java
  (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryClientUtils.java
  Mon Feb 25 00:44:40 2008
@@ -29,6 +29,7 @@
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.ArrayList;
 
 /**
  * This class is used to provide client side utilities when someone uses 
Remote registry If user
@@ -75,8 +76,9 @@
     public static void exportFromRegistry(File toFile, String path, Registry 
registry)
             throws RegistryException {
         try {
-            processExport(path, toFile, registry);
+            processExport(path, toFile, registry ,true);
         } catch (Exception e) {
+            e.printStackTrace();
             throw new RegistryException(e.getMessage());
         }
     }
@@ -105,56 +107,100 @@
         }
     }
 
-    private static  void processExport(String resourcePath,
-                                       File toFile, Registry registry) throws 
RegistryException {
-        Resource resource = registry.get(resourcePath);
+
+    private static void processExport(String fromPath,
+                             File toFile ,
+                             Registry registry ,
+                             boolean useOrginal ) throws RegistryException {
+        Resource resource = registry.get(fromPath);
         if (resource != null) {
+
+            String resourcePath = resource.getPath();
+            int verionIndex = resourcePath.indexOf("?");
+            if (verionIndex >0) {
+                resourcePath = resourcePath.substring(0,verionIndex);
+            }
+            int slashIndex = resourcePath.lastIndexOf("/");
+            //getting only the last part of the resource path
+            resourcePath = 
resourcePath.substring(slashIndex,resourcePath.length());
+            String currentPath = null;
+            File tempFile ;
+            if (!useOrginal) {
+                tempFile = new File(toFile ,resourcePath);
+                if (!tempFile.exists() && resource.isDirectory()) {
+                    tempFile.mkdirs();
+                }
+            } else {
+                tempFile = toFile;
+            }
             if (resource.isDirectory()) {
-                String currentResourcePath = resource.getPath();
-                int verionIndex = currentResourcePath.indexOf("?");
-                if (verionIndex >0) {
-                    currentResourcePath = 
currentResourcePath.substring(0,verionIndex);
-                }
-                File file = new File(toFile, currentResourcePath);
-                if (file.exists()) {
-                    log.info("No need to create the directory " + 
currentResourcePath);
-                } else {
-                    boolean fileFlag = file.mkdirs();
-                    if (!fileFlag) {
-                        log.info("Unable to create the directory " + 
currentResourcePath);
+                String childNodes[] = (String[])resource.getContent();
+                ArrayList tobeDeleted = new ArrayList();
+                String [] files = tempFile.list();
+                for (int i = 0; i < files.length; i++) {
+                    String file = files[i];
+                    tobeDeleted.add("/" +  file);
+                }
+                for (int i = 0; i < childNodes.length; i++) {
+                    String childNode = childNodes[i];
+                    verionIndex = childNode.indexOf("?");
+                    if (verionIndex >0) {
+                        childNode = childNode.substring(0,verionIndex);
+                    }
+                    slashIndex = childNode.lastIndexOf("/");
+                    //getting only the last part of the resource path
+                    childNode = 
childNode.substring(slashIndex,childNode.length());
+                    tobeDeleted.remove(childNode);
+                }
+                for (int i = 0; i < tobeDeleted.size(); i++) {
+                    String s = (String) tobeDeleted.get(i);
+                    slashIndex = s.lastIndexOf("/");
+                    //getting only the last part of the resource path
+                    s = s.substring(slashIndex,s.length());
+                    File deleteFile = new File(tempFile ,s);
+                    if (deleteFile.exists() && deleteFile.isDirectory()){
+                        deleteDir(deleteFile);
+                        log.info("Deleting a directoy : "  + 
deleteFile.getPath());
+                    } else {
+                        deleteFile.delete();
+                        log.info("Deleting a file : "  + deleteFile.getPath());
                     }
                 }
 
-                String childNodes[] = (String[])resource.getContent();
+
                 for (String childNode : childNodes) {
-                    processExport(childNode, toFile, registry);
+                    processExport(childNode, tempFile, registry ,false);
                 }
             } else {
-                String currentResourcePath = resource.getPath();
-                int verionIndex = currentResourcePath.indexOf("?");
-                if (verionIndex >0) {
-                    currentResourcePath = 
currentResourcePath.substring(0,verionIndex);
-                }
-                File file = new File(toFile, currentResourcePath);
                 try {
-                    if (file.exists()) {
-                        log.info("No need to create the file " + 
currentResourcePath);
-                    } else {
-                        boolean fileFlag = file.mkdirs();
-                        if (!fileFlag) {
-                            log.info("Unable to create the file " + 
currentResourcePath);
-                        }
-                    }
-                    FileOutputStream out = new FileOutputStream(file);
+                    FileOutputStream out = new FileOutputStream(tempFile);
                     out.write((byte[])resource.getContent());
                     out.flush();
                     out.close();
                 } catch (IOException e) {
-                    throw new RegistryException(Messages.getMessage(
-                            "error.creating.file", currentResourcePath));
+                     throw new RegistryException(Messages.getMessage(
+                            "error.creating.file", currentPath));
                 }
+            }
+        } else {
+            log.info("No resource found for : "  + fromPath);
+        }
+    }
 
+    private static boolean deleteDir(File dir) {
+        if (dir.isDirectory()) {
+            String[] children = dir.list();
+            for (int i=0; i<children.length; i++) {
+                boolean success = deleteDir(new File(dir, children[i]));
+                if (!success) {
+                    return false;
+                }
             }
         }
+        // The directory is now empty so delete it
+        return dir.delete();
     }
+
+
+
 }

_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev

Reply via email to