sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1630737 - in /sling/trunk/samples/slingshot/src/main: java/org/apache/sling/sample/slingshot/ java/org/apache/sling/sample/slingshot/impl/ resources/SLING-INF/content/libs/slingshot/Item/
Date Fri, 10 Oct 2014 08:52:18 GMT
Author: cziegeler
Date: Fri Oct 10 08:52:17 2014
New Revision: 1630737

URL: http://svn.apache.org/r1630737
Log:
Correct commenting component and add rating component

Modified:
    sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotConstants.java
    sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotUtil.java
    sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/CommentPostServlet.java
    sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/RatingPostServlet.java
    sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/Util.java
    sling/trunk/samples/slingshot/src/main/resources/SLING-INF/content/libs/slingshot/Item/html.jsp

Modified: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotConstants.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotConstants.java?rev=1630737&r1=1630736&r2=1630737&view=diff
==============================================================================
--- sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotConstants.java
(original)
+++ sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotConstants.java
Fri Oct 10 08:52:17 2014
@@ -54,7 +54,9 @@ public abstract class SlingshotConstants
 
     public static final String PROPERTY_TAGS = "tags";
 
-    public static final String PROPERTY_USER = "jcr:createdBy";
+    public static final String PROPERTY_USER = "user";
 
     public static final String PROPERTY_CREATED = "jcr:created";
+
+    public static final String PROPERTY_RATING = "rating";
 }

Modified: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotUtil.java?rev=1630737&r1=1630736&r2=1630737&view=diff
==============================================================================
--- sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotUtil.java
(original)
+++ sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/SlingshotUtil.java
Fri Oct 10 08:52:17 2014
@@ -16,7 +16,15 @@
  */
 package org.apache.sling.sample.slingshot;
 
+import java.util.HashMap;
+import java.util.Map;
+
+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.ResourceUtil;
+import org.apache.sling.api.resource.ValueMap;
 
 public abstract class SlingshotUtil {
 
@@ -37,4 +45,53 @@ public abstract class SlingshotUtil {
         }
         return id;
     }
+
+    public static int getRating(final Resource resource) {
+        int rating = 0;
+
+        final Resource ratingsResource = resource.getChild("ratings");
+        if ( ratingsResource != null ) {
+            int count = 0;
+            for(final Resource r : ratingsResource.getChildren()) {
+                final ValueMap vm = r.getValueMap();
+                final int current = vm.get(SlingshotConstants.PROPERTY_RATING, 0);
+                rating += current;
+                count++;
+            }
+            if ( count > 0 ) {
+                rating = rating / count;
+            }
+        }
+        return rating;
+    }
+
+    public static int getOwnRating(final Resource resource, final String userId) {
+        int rating = 0;
+
+        final Resource r = resource.getResourceResolver().getResource(resource.getParent()
+ "/ratings/" + userId);
+        if ( r != null ) {
+            final ValueMap vm = r.getValueMap();
+            rating = vm.get(SlingshotConstants.PROPERTY_RATING, 0);
+        }
+        return rating;
+
+    }
+
+    public static void setOwnRating(final Resource resource, final String userId, final int
rating)
+    throws PersistenceException {
+        final Map<String, Object> props = new HashMap<String, Object>();
+        props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, SlingshotConstants.RESOURCETYPE_RATINGS);
+        final Resource ratingsResource = ResourceUtil.getOrCreateResource(resource.getResourceResolver(),
+                resource.getPath() + "/ratings", props, null, true);
+
+        props.clear();
+        props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, SlingshotConstants.RESOURCETYPE_RATING);
+        final Resource r = ResourceUtil.getOrCreateResource(resource.getResourceResolver(),
+                ratingsResource.getPath() + "/" + userId, props, null, false);
+
+        final ModifiableValueMap mv = r.adaptTo(ModifiableValueMap.class);
+        mv.put(SlingshotConstants.PROPERTY_RATING, rating);
+
+        r.getResourceResolver().commit();
+    }
 }

Modified: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/CommentPostServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/CommentPostServlet.java?rev=1630737&r1=1630736&r2=1630737&view=diff
==============================================================================
--- sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/CommentPostServlet.java
(original)
+++ sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/CommentPostServlet.java
Fri Oct 10 08:52:17 2014
@@ -58,19 +58,19 @@ public class CommentPostServlet extends 
 
         final String userId = request.getRemoteUser();
 
+        logger.debug("New comment from {} : {} - {}", new Object[] {userId, title, description});
         // TODO - check values
 
         // save comment
         ResourceResolver resolver = null;
         try {
-            final Map<String, Object> loginmap = new HashMap<String, Object>();
-            loginmap.put(ResourceResolverFactory.USER_IMPERSONATION, userId);
-            resolver = factory.getAdministrativeResourceResolver(loginmap);
+            resolver = factory.getAdministrativeResourceResolver(null);
 
             final Map<String, Object> properties = new HashMap<String, Object>();
             properties.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, SlingshotConstants.RESOURCETYPE_COMMENT);
             properties.put(SlingshotConstants.PROPERTY_TITLE, title);
             properties.put(SlingshotConstants.PROPERTY_DESCRIPTION, description);
+            properties.put(SlingshotConstants.PROPERTY_USER, userId);
 
             // we try it five times
             PersistenceException exception = null;

Modified: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/RatingPostServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/RatingPostServlet.java?rev=1630737&r1=1630736&r2=1630737&view=diff
==============================================================================
--- sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/RatingPostServlet.java
(original)
+++ sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/RatingPostServlet.java
Fri Oct 10 08:52:17 2014
@@ -17,8 +17,7 @@
 package org.apache.sling.sample.slingshot.impl;
 
 import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
+import java.io.PrintWriter;
 
 import javax.servlet.ServletException;
 
@@ -31,6 +30,7 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.api.servlets.SlingAllMethodsServlet;
 import org.apache.sling.sample.slingshot.SlingshotConstants;
+import org.apache.sling.sample.slingshot.SlingshotUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,28 +48,18 @@ public class RatingPostServlet extends S
     protected void doPost(final SlingHttpServletRequest request,
             final SlingHttpServletResponse response)
     throws ServletException, IOException {
-        final String title = request.getParameter(SlingshotConstants.PROPERTY_TITLE);
-        final String description = request.getParameter(SlingshotConstants.PROPERTY_DESCRIPTION);
+        final String rating = request.getParameter(SlingshotConstants.PROPERTY_RATING);
 
         final String userId = request.getRemoteUser();
 
-        // TODO - check values
+        logger.info("New rating from {} : {}", userId, rating);
 
         // save comment
         ResourceResolver resolver = null;
         try {
-            final Map<String, Object> loginmap = new HashMap<String, Object>();
-            loginmap.put(ResourceResolverFactory.USER_IMPERSONATION, userId);
-            resolver = factory.getAdministrativeResourceResolver(loginmap);
-
-            final Map<String, Object> properties = new HashMap<String, Object>();
-            properties.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, SlingshotConstants.RESOURCETYPE_COMMENT);
-            properties.put(SlingshotConstants.PROPERTY_TITLE, title);
-            properties.put(SlingshotConstants.PROPERTY_DESCRIPTION, description);
+            resolver = factory.getAdministrativeResourceResolver(null);
 
-            resolver.create(request.getResource(), "bla", properties);
-
-            resolver.commit();
+            SlingshotUtil.setOwnRating(request.getResource().getParent(), userId, Integer.valueOf(rating));
         } catch ( final LoginException le ) {
             throw new ServletException("Unable to login", le);
         } finally {
@@ -78,10 +68,15 @@ public class RatingPostServlet extends S
             }
         }
 
-        // send redirect at the end
-        final String path = request.getResource().getParent().getPath();
-
-        response.sendRedirect(request.getContextPath() + path + ".html");
+        response.setContentType("application/json");
+        response.setCharacterEncoding("utf-8");
+        response.setStatus(200);
+
+        final PrintWriter pw = response.getWriter();
+        pw.print("{ ");
+        pw.print(" \"rating\" : ");
+        pw.print(String.valueOf(SlingshotUtil.getRating(request.getResource().getParent())));
+        pw.println("}");
     }
 
 }

Modified: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/Util.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/Util.java?rev=1630737&r1=1630736&r2=1630737&view=diff
==============================================================================
--- sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/Util.java
(original)
+++ sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/Util.java
Fri Oct 10 08:52:17 2014
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.sample.slingshot.impl;
 
+
 public abstract class Util {
 
     private static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789_";
@@ -51,4 +52,6 @@ public abstract class Util {
 
         return sb.toString();
     }
+
+
 }

Modified: sling/trunk/samples/slingshot/src/main/resources/SLING-INF/content/libs/slingshot/Item/html.jsp
URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/src/main/resources/SLING-INF/content/libs/slingshot/Item/html.jsp?rev=1630737&r1=1630736&r2=1630737&view=diff
==============================================================================
--- sling/trunk/samples/slingshot/src/main/resources/SLING-INF/content/libs/slingshot/Item/html.jsp
(original)
+++ sling/trunk/samples/slingshot/src/main/resources/SLING-INF/content/libs/slingshot/Item/html.jsp
Fri Oct 10 08:52:17 2014
@@ -45,20 +45,52 @@
     <sling:include resource="<%= resource %>" replaceSelectors="trail"/>
     <h1><%= title %></h1>
     <div style="width:100%; display:block; float:left;">
-    <div style="width:70%; float:left; display:block;">
+      <div style="width:70%; float:left; display:block;">
         <img src="<%= request.getContextPath() %><%= imagePath %>"/>
-    </div>
-    <div style="width:30%; float:left; padding:15px; display:block;">
+      </div>
+      <div style="width:30%; float:left; padding:15px; display:block;">
         <p><%=ResponseUtil.escapeXml(attributes.get(SlingshotConstants.PROPERTY_DESCRIPTION,
""))%></p>
         <% if ( attributes.get(SlingshotConstants.PROPERTY_LOCATION) != null ) { %>
             <p>Location</p>
             <p><%=ResponseUtil.escapeXml(attributes.get(SlingshotConstants.PROPERTY_LOCATION,
""))%></p>
         <% } %>
-       <% if ( slingRequest.getRemoteUser() != null && slingRequest.getRemoteUser().equals(SlingshotUtil.getUserId(resource))
)  { %>
+        <% if ( slingRequest.getRemoteUser() != null && slingRequest.getRemoteUser().equals(SlingshotUtil.getUserId(resource))
)  { %>
         <button class="ui-button ui-form-button ui-slingshot-clickable" 
                 data-link="<%= request.getContextPath() %><%=resource.getName()
%>.edit.html" type="button">Edit</button>
-       <% } %>
-    </div>
+        <% } %>
+        <div class="metro">
+          <div id="rating" 
+               data-score-hint="Rating: " 
+               data-show-score="true" 
+               data-role="rating" 
+               data-stars="5" 
+               data-score="<%= SlingshotUtil.getRating(resource) %>" 
+               data-static="false" 
+               class="rating large" style="height: auto;">
+               <ul><li></li><li></li><li></li><li></li><li></li></ul>
+               <span class="score-hint">Rating: <%= SlingshotUtil.getRating(resource)
%></span>
+          </div>
+          <div class="fg-green rating active" id="own_rating" style="height: auto;">
+          <ul><li title="bad" class="rated"></li><li title="poor"></li><li
title="regular"></li><li title="good"></li><li title="gorgeous"></li></ul><span
class="score-hint">Current score: <%= SlingshotUtil.getOwnRating(resource, request.getRemoteUser())
%></span></div>
+                    <script>
+                        $(function(){
+                            $("#own_rating").rating({
+                                static: false,
+                                score: <%= SlingshotUtil.getOwnRating(resource, request.getRemoteUser())
%>,
+                                stars: 5,
+                                showHint: true,
+                                showScore: true,
+                                click: function(value, rating) {
+                                    rating.rate(value);
+                                	$.post( "<%= resource.getName() %>/ratings", { <%=
SlingshotConstants.PROPERTY_RATING %> : value }, function( data ) {
+                                		  $("#rating").rating("rate", data.rating);
+                                		}, "json");
+                                }
+                            });
+                        });
+                    </script>
+        </div>
+      </div>
     </div>
     <hr/>
     <sling:include resource="<%= resource %>" replaceSelectors="comments"/>



Mime
View raw message