svn commit: r638560 - in /ofbiz/trunk/framework: base/src/base/org/ofbiz/base/util/ webtools/src/org/ofbiz/webtools/artifactinfo/ widget/src/org/ofbiz/widget/form/ widget/src/org/ofbiz/widget/screen/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r638560 - in /ofbiz/trunk/framework: base/src/base/org/ofbiz/base/util/ webtools/src/org/ofbiz/webtools/artifactinfo/ widget/src/org/ofbiz/widget/form/ widget/src/org/ofbiz/widget/screen/

jacopoc
Author: jacopoc
Date: Tue Mar 18 13:47:51 2008
New Revision: 638560

URL: http://svn.apache.org/viewvc?rev=638560&view=rev
Log:
Implemented initial work on the retrieval of all the form and screen definitions for the artifact info classes.

Modified:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/FileUtil.java
    ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java
    ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java
    ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFactory.java

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/FileUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/FileUtil.java?rev=638560&r1=638559&r2=638560&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/FileUtil.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/FileUtil.java Tue Mar 18 13:47:51 2008
@@ -19,6 +19,7 @@
 package org.ofbiz.base.util;
 
 import java.io.*;
+import java.util.List;
 
 /**
  * File Utilities
@@ -93,8 +94,7 @@
         return fileName;
     }
 
-    public static StringBuffer readTextFile(String fileName, boolean newline) throws FileNotFoundException, IOException {
-        File file = new File(fileName);
+    public static StringBuffer readTextFile(File file, boolean newline) throws FileNotFoundException, IOException {
         if (!file.exists()) {
             throw new FileNotFoundException();
         }
@@ -125,5 +125,25 @@
         }
         
         return buf;
+    }
+    public static StringBuffer readTextFile(String fileName, boolean newline) throws FileNotFoundException, IOException {
+        File file = new File(fileName);
+        return readTextFile(file, newline);
+    }
+
+    public static void searchFiles(List fileList, File path, FilenameFilter filter, boolean includeSubfolders) throws IOException {
+        // Get filtered files in the current path
+        File[] files = path.listFiles(filter);
+        
+        // Process each filtered entry
+        for (int i = 0; i < files.length; i++) {
+            // recurse if the entry is a directory
+            if (files[i].isDirectory() && includeSubfolders && !files[i].getName().startsWith(".")) {
+                searchFiles(fileList, files[i], filter, true);
+            } else {
+                // add the filtered file to the list
+                fileList.add(files[i]);
+            }
+        }
     }
 }

Modified: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java?rev=638560&r1=638559&r2=638560&view=diff
==============================================================================
--- ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java (original)
+++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ArtifactInfoFactory.java Tue Mar 18 13:47:51 2008
@@ -18,9 +18,14 @@
  */
 package org.ofbiz.webtools.artifactinfo;
 
+import java.io.File;
+import java.io.FilenameFilter;
 import java.io.IOException;
+import java.io.FileNotFoundException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -29,9 +34,12 @@
 
 import javolution.util.FastMap;
 import javolution.util.FastSet;
+import javolution.util.FastList;
 
+import org.ofbiz.base.component.ComponentConfig;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.GeneralException;
+import org.ofbiz.base.util.FileUtil;
 import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.base.util.cache.UtilCache;
 import org.ofbiz.entity.GenericEntityException;
@@ -56,9 +64,9 @@
  *
  */
 public class ArtifactInfoFactory {
-    
+
     public static final String module = ArtifactInfoFactory.class.getName();
-    
+
     protected static UtilCache<String, ArtifactInfoFactory> artifactInfoFactoryCache = new UtilCache("ArtifactInfoFactory");
     
     public static final String EntityInfoTypeId = "entity";
@@ -134,11 +142,59 @@
         }
         
         // how to get all Service ECAs to prepare? don't worry about it, will be populated from service load, ie all ECAs for each service
-        
-        // TODO: how to get all forms to prepare?
-        
-        // TODO: how to get all screens to prepare?
-        
+
+        Collection<ComponentConfig> componentConfigs = ComponentConfig.getAllComponents();
+        for (ComponentConfig componentConfig: componentConfigs) {
+            String componentName = componentConfig.getGlobalName();
+            String rootComponentPath = componentConfig.getRootLocation();
+            List<File> screenFiles = null;
+            List<File> formFiles = null;
+            try {
+                screenFiles = this.findScreenWidgetDefinitionFiles(rootComponentPath);
+                formFiles = this.findFormWidgetDefinitionFiles(rootComponentPath);
+            } catch(IOException ioe) {
+                throw new GeneralException(ioe.getMessage());
+            }
+            if (screenFiles != null) {
+                for (File screenFile: screenFiles) {
+                    String screenFilePath = screenFile.getAbsolutePath();
+                    screenFilePath = screenFilePath.replace('\\', '/');
+                    String screenFileRelativePath = screenFilePath.substring(rootComponentPath.length());
+                    String screenLocation = "component://" + componentName + "/" + screenFileRelativePath;
+                    Map modelScreenMap = null;
+                    try {
+                        modelScreenMap = ScreenFactory.getScreensFromLocation(screenLocation);
+                    } catch(Exception exc) {
+                        throw new GeneralException(exc.getMessage());
+                    }
+                    Iterator screenNames = modelScreenMap.keySet().iterator();
+                    while (screenNames.hasNext()) {
+                        String screenName = (String)screenNames.next();
+                        this.getScreenWidgetArtifactInfo(screenName, screenLocation);
+                    }
+                }
+            }
+            if (formFiles != null) {
+                for (File formFile: formFiles) {
+                    String formFilePath = formFile.getAbsolutePath();
+                    formFilePath = formFilePath.replace('\\', '/');
+                    String formFileRelativePath = formFilePath.substring(rootComponentPath.length());
+                    String formLocation = "component://" + componentName + "/" + formFileRelativePath;
+                    Map modelFormMap = null;
+                    try {
+                        modelFormMap = FormFactory.getFormsFromLocation(formLocation, this.getEntityModelReader(), this.getDispatchContext());
+                    } catch(Exception exc) {
+                        throw new GeneralException(exc.getMessage());
+                    }
+                    Iterator formNames = modelFormMap.keySet().iterator();
+                    while (formNames.hasNext()) {
+                        String formName = (String)formNames.next();
+                        this.getFormWidgetArtifactInfo(formName, formLocation);
+                    }
+                }
+            }
+        }
+
         // TODO: get all controller requests and views to prepare
         Set<URL> controllerUrlSet = FastSet.newInstance();
         for (URL controllerUrl: controllerUrlSet) {
@@ -323,4 +379,79 @@
         
         return aiBaseSet;
     }
+    
+    public static List<File> findFormWidgetDefinitionFiles(String basePath) throws IOException {
+        if (basePath == null) {
+            basePath = System.getProperty("ofbiz.home");
+        }
+        List<File> fileList = FastList.newInstance();
+        FileUtil.searchFiles(fileList, new File(basePath), new FilenameFilter() {
+            public boolean accept(File dir, String name) {
+                File file = new File(dir, name);
+                if (file.getName().startsWith(".")) {
+                    return false;
+                }
+                if (file.isDirectory()) {
+                    return true;
+                }
+                if (name.endsWith(".xml")) {
+                    String xmlFile = null;
+                    try {
+                        xmlFile = FileUtil.readTextFile(file, true).toString();
+                    } catch (FileNotFoundException e) {
+                        Debug.logWarning("Error reading xml file [" + file + "] for service implementation: " + e.toString(), module);
+                        return false;
+                    } catch (IOException e) {
+                        Debug.logWarning("Error reading xml file [" + file + "] for service implementation: " + e.toString(), module);
+                        return false;
+                    }
+                    if (UtilValidate.isNotEmpty(xmlFile)) {
+                        return xmlFile.indexOf("<forms ") > 0 && xmlFile.indexOf("widget-form.xsd") > 0;
+                    }
+                } else {
+                    return false;
+                }
+                return false;
+            }
+        }, true);
+        return fileList;
+    }
+
+    public static List<File> findScreenWidgetDefinitionFiles(String basePath) throws IOException {
+        if (basePath == null) {
+            basePath = System.getProperty("ofbiz.home");
+        }
+        List<File> fileList = FastList.newInstance();
+        FileUtil.searchFiles(fileList, new File(basePath), new FilenameFilter() {
+            public boolean accept(File dir, String name) {
+                File file = new File(dir, name);
+                if (file.getName().startsWith(".")) {
+                    return false;
+                }
+                if (file.isDirectory()) {
+                    return true;
+                }
+                if (name.endsWith(".xml")) {
+                    String xmlFile = null;
+                    try {
+                        xmlFile = FileUtil.readTextFile(file, true).toString();
+                    } catch (FileNotFoundException e) {
+                        Debug.logWarning("Error reading xml file [" + file + "] for service implementation: " + e.toString(), module);
+                        return false;
+                    } catch (IOException e) {
+                        Debug.logWarning("Error reading xml file [" + file + "] for service implementation: " + e.toString(), module);
+                        return false;
+                    }
+                    if (UtilValidate.isNotEmpty(xmlFile)) {
+                        return xmlFile.indexOf("<screens ") > 0 && xmlFile.indexOf("widget-screen.xsd") > 0;
+                    }
+                } else {
+                    return false;
+                }
+                return false;
+            }
+        }, true);
+        return fileList;
+    }
+
 }

Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java?rev=638560&r1=638559&r2=638560&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java (original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java Tue Mar 18 13:47:51 2008
@@ -52,7 +52,7 @@
     public static final UtilCache formLocationCache = new UtilCache("widget.form.locationResource", 0, 0, false);
     public static final UtilCache formWebappCache = new UtilCache("widget.form.webappResource", 0, 0, false);
     
-    public static ModelForm getFormFromLocation(String resourceName, String formName, ModelReader entityModelReader, DispatchContext dispatchContext)
+    public static Map getFormsFromLocation(String resourceName, ModelReader entityModelReader, DispatchContext dispatchContext)
             throws IOException, SAXException, ParserConfigurationException {
         Map modelFormMap = (Map) formLocationCache.get(resourceName);
         if (modelFormMap == null) {
@@ -72,14 +72,21 @@
                 }
             }
         }
-        
+
+        return modelFormMap;
+    }
+
+    public static ModelForm getFormFromLocation(String resourceName, String formName, ModelReader entityModelReader, DispatchContext dispatchContext)
+            throws IOException, SAXException, ParserConfigurationException {
+        Map modelFormMap = getFormsFromLocation(resourceName, entityModelReader, dispatchContext);
+
         ModelForm modelForm = (ModelForm) modelFormMap.get(formName);
         if (modelForm == null) {
             throw new IllegalArgumentException("Could not find form with name [" + formName + "] in class resource [" + resourceName + "]");
         }
         return modelForm;
     }
-    
+
     public static ModelForm getFormFromWebappContext(String resourceName, String formName, HttpServletRequest request)
             throws IOException, SAXException, ParserConfigurationException {
         String webappName = UtilHttp.getApplicationName(request);

Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFactory.java?rev=638560&r1=638559&r2=638560&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFactory.java (original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFactory.java Tue Mar 18 13:47:51 2008
@@ -95,6 +95,16 @@
     
     public static ModelScreen getScreenFromLocation(String resourceName, String screenName)
             throws IOException, SAXException, ParserConfigurationException {
+        Map modelScreenMap = getScreensFromLocation(resourceName);
+        ModelScreen modelScreen = (ModelScreen) modelScreenMap.get(screenName);
+        if (modelScreen == null) {
+            throw new IllegalArgumentException("Could not find screen with name [" + screenName + "] in class resource [" + resourceName + "]");
+        }
+        return modelScreen;
+    }
+
+    public static Map getScreensFromLocation(String resourceName)
+            throws IOException, SAXException, ParserConfigurationException {
         Map modelScreenMap = (Map) screenLocationCache.get(resourceName);
         if (modelScreenMap == null) {
             synchronized (ScreenFactory.class) {
@@ -120,13 +130,12 @@
             }
         }
         
-        ModelScreen modelScreen = (ModelScreen) modelScreenMap.get(screenName);
-        if (modelScreen == null) {
-            throw new IllegalArgumentException("Could not find screen with name [" + screenName + "] in class resource [" + resourceName + "]");
+        if (modelScreenMap == null) {
+            throw new IllegalArgumentException("Could not find screen file with name [" + resourceName + "]");
         }
-        return modelScreen;
+        return modelScreenMap;
     }
-    
+
     public static ModelScreen getScreenFromWebappContext(String resourceName, String screenName, HttpServletRequest request)
             throws IOException, SAXException, ParserConfigurationException {
         String webappName = UtilHttp.getApplicationName(request);