incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1464397 - in /sling/trunk/samples/slingshot: pom.xml src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java
Date Thu, 04 Apr 2013 09:35:32 GMT
Author: cziegeler
Date: Thu Apr  4 09:35:32 2013
New Revision: 1464397

URL: http://svn.apache.org/r1464397
Log:
Update sample to use CRUD api

Modified:
    sling/trunk/samples/slingshot/pom.xml
    sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java

Modified: sling/trunk/samples/slingshot/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/pom.xml?rev=1464397&r1=1464396&r2=1464397&view=diff
==============================================================================
--- sling/trunk/samples/slingshot/pom.xml (original)
+++ sling/trunk/samples/slingshot/pom.xml Thu Apr  4 09:35:32 2013
@@ -112,7 +112,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.1.0</version>
+            <version>2.3.0</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java?rev=1464397&r1=1464396&r2=1464397&view=diff
==============================================================================
--- sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java
(original)
+++ sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java
Thu Apr  4 09:35:32 2013
@@ -26,35 +26,40 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.resource.LoginException;
-import org.apache.sling.api.resource.PersistableValueMap;
+import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.sample.slingshot.Constants;
 import org.osgi.service.event.Event;
+import org.osgi.service.event.EventConstants;
 import org.osgi.service.event.EventHandler;
+import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * This is a samle for observation which adds tags to new resources.
+ * This is a sample for observation which adds tags to new resources.
  */
 @Component(immediate=true)
 @Service(value=EventHandler.class)
 @Properties({
    @Property(name="service.description",
              value="Apache Sling - Slingshot Tagging Service"),
-   @Property(name="event.topics", value=org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED)
+   @Property(name=EventConstants.EVENT_TOPIC, value=org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED)
 })
 public class AutomaticTaggingService
     implements EventHandler {
 
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
     @Reference
     private ResourceResolverFactory resourceResolverFactory;
 
     private final Random random = new Random(System.currentTimeMillis());
 
     private final String MIXIN_TYPE_PROPERTY = "jcr:mixinTypes";
+    private final String MIXIN_TYPE_PHOTO = "slingshot:Photo";
 
     /**
      * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event)
@@ -67,16 +72,16 @@ public class AutomaticTaggingService
                 resolver = this.resourceResolverFactory.getAdministrativeResourceResolver(null);
                 final Resource r = resolver.getResource(path);
                 if ( r != null && r.isResourceType(Constants.RESOURCETYPE_PHOTO)
) {
-                    final PersistableValueMap pvm = r.adaptTo(PersistableValueMap.class);
-                    if ( pvm != null ) {
-                        String[] types = pvm.get(MIXIN_TYPE_PROPERTY, String[].class);
+                    final ModifiableValueMap mvm = r.adaptTo(ModifiableValueMap.class);
+                    if ( mvm != null ) {
+                        String[] types = mvm.get(MIXIN_TYPE_PROPERTY, String[].class);
                         if ( types == null ) {
-                            pvm.put(MIXIN_TYPE_PROPERTY, "slingshot:Photo");
+                            mvm.put(MIXIN_TYPE_PROPERTY, MIXIN_TYPE_PHOTO);
                         } else {
                             String[] newTypes = new String[types.length + 1];
                             System.arraycopy(types, 0, newTypes, 0, types.length);
-                            newTypes[types.length] = "slingshot:Photo";
-                            pvm.put(MIXIN_TYPE_PROPERTY, newTypes);
+                            newTypes[types.length] = MIXIN_TYPE_PHOTO;
+                            mvm.put(MIXIN_TYPE_PROPERTY, newTypes);
                         }
 
                         final int tagsValue = this.random.nextInt(8);
@@ -90,12 +95,12 @@ public class AutomaticTaggingService
                         if ( (tagsValue & 4) == 4 ) {
                             tags.add("Cool");
                         }
-                        pvm.put(Constants.PROPERTY_SLINGSHOT_TAGS, tags.toArray(new String[tags.size()]));
+                        mvm.put(Constants.PROPERTY_SLINGSHOT_TAGS, tags.toArray(new String[tags.size()]));
                         try {
-                            pvm.save();
-                        } catch (PersistenceException e) {
-                            LoggerFactory.getLogger(this.getClass()).info("Ups", e);
+                            resolver.commit();
+                        } catch (final PersistenceException e) {
                             // we just ignore this for now
+                            logger.info("Unable to add tags to photo: " + r.getPath(), e);
                         }
                     }
                 }



Mime
View raw message