Sebastian Sdorra avatar Sebastian Sdorra committed cc464e9

added getAll method to data store api

Comments (0)

Files changed (2)

scm-core/src/main/java/sonia/scm/store/DataStore.java

 
 package sonia.scm.store;
 
+//~--- JDK imports ------------------------------------------------------------
+
+import java.util.Map;
+
 /**
  *
  * @author Sebastian Sdorra
    * @return
    */
   public T get(String id);
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  public Map<String, T> getAll();
 }

scm-dao-xml/src/main/java/sonia/scm/store/JAXBDataStore.java

 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
+import com.google.common.collect.Maps;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.File;
 
+import java.util.Map;
+
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
   {
     logger.trace("try to retrieve item with id {}", id);
 
+    File file = getFile(id);
+
+    return read(file);
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  @Override
+  public Map<String, T> getAll()
+  {
+    logger.trace("get all items from data store");
+
+    Builder<String, T> builder = ImmutableMap.builder();
+
+    for (File file : directory.listFiles())
+    {
+      builder.put(getId(file), read(file));
+    }
+
+    return builder.build();
+  }
+
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param file
+   *
+   * @return
+   */
+  private T read(File file)
+  {
     T item = null;
-    File file = getFile(id);
 
     if (file.exists())
     {
+      logger.trace("try to read {}", file);
       try
       {
         item = (T) context.createUnmarshaller().unmarshal(file);
       }
       catch (JAXBException ex)
       {
-        throw new StoreException("could not read object with id ".concat(id),
-          ex);
+        throw new StoreException(
+          "could not read object ".concat(file.getPath()), ex);
       }
     }
 
     return item;
   }
 
-  //~--- methods --------------------------------------------------------------
-
   /**
    * Method description
    *
     return new File(directory, id.concat(SUFFIX));
   }
 
+  /**
+   * Method description
+   *
+   *
+   * @param file
+   *
+   * @return
+   */
+  private String getId(File file)
+  {
+    String name = file.getName();
+
+    return name.substring(0, name.length() - SUFFIX.length());
+  }
+
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.