rave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ja...@apache.org
Subject svn commit: r1189099 - in /incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal: model/Widget.java repository/WidgetRepository.java repository/impl/JpaWidgetRepository.java
Date Wed, 26 Oct 2011 09:54:54 GMT
Author: jasha
Date: Wed Oct 26 09:54:53 2011
New Revision: 1189099

URL: http://svn.apache.org/viewvc?rev=1189099&view=rev
Log:
RAVE-301 prepare for dynamic sorting in Widget query. By default sort on title ASC in all
lists.

Modified:
    incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Widget.java
    incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/WidgetRepository.java
    incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaWidgetRepository.java

Modified: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Widget.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Widget.java?rev=1189099&r1=1189098&r2=1189099&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Widget.java
(original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/model/Widget.java
Wed Oct 26 09:54:53 2011
@@ -50,24 +50,19 @@ import java.util.List;
 @Entity
 @Table(name="widget")
 @NamedQueries({
-        @NamedQuery(name = Widget.WIDGET_GET_ALL, query = Widget.SELECT_W_FROM_WIDGET_W),
+        @NamedQuery(name = Widget.WIDGET_GET_ALL, query = Widget.SELECT_W_FROM_WIDGET_W +
Widget.ORDER_BY_TITLE_ASC),
         @NamedQuery(name = Widget.WIDGET_COUNT_ALL, query = Widget.SELECT_COUNT_W_FROM_WIDGET_W),
 
         @NamedQuery(name = Widget.WIDGET_GET_BY_FREE_TEXT,
-                query = Widget.SELECT_W_FROM_WIDGET_W + Widget.WHERE_CLAUSE_FREE_TEXT),
+                query = Widget.SELECT_W_FROM_WIDGET_W + Widget.WHERE_CLAUSE_FREE_TEXT + Widget.ORDER_BY_TITLE_ASC),
         @NamedQuery(name = Widget.WIDGET_COUNT_BY_FREE_TEXT,
                 query = Widget.SELECT_COUNT_W_FROM_WIDGET_W + Widget.WHERE_CLAUSE_FREE_TEXT),
 
         @NamedQuery(name = Widget.WIDGET_GET_BY_STATUS,
-                query = Widget.SELECT_W_FROM_WIDGET_W + Widget.WHERE_CLAUSE_STATUS),
+                query = Widget.SELECT_W_FROM_WIDGET_W + Widget.WHERE_CLAUSE_STATUS + Widget.ORDER_BY_TITLE_ASC),
         @NamedQuery(name = Widget.WIDGET_COUNT_BY_STATUS,
                 query = Widget.SELECT_COUNT_W_FROM_WIDGET_W + Widget.WHERE_CLAUSE_STATUS),
 
-        @NamedQuery(name = Widget.WIDGET_GET_BY_STATUS_AND_TYPE_AND_FREE_TEXT,
-                query = Widget.SELECT_W_FROM_WIDGET_W + Widget.WHERE_CLAUSE_STATUS_TYPE_FREE_TEXT),
-        @NamedQuery(name = Widget.WIDGET_COUNT_BY_STATUS_AND_TYPE_AND_FREE_TEXT,
-                query = Widget.SELECT_COUNT_W_FROM_WIDGET_W + Widget.WHERE_CLAUSE_STATUS_TYPE_FREE_TEXT),
-        
         @NamedQuery(name = Widget.WIDGET_GET_BY_URL, query = Widget.SELECT_W_FROM_WIDGET_W
+ Widget.WHERE_CLAUSE_URL)
 })
 public class Widget implements BasicEntity, Serializable {
@@ -75,7 +70,6 @@ public class Widget implements BasicEnti
 
     public static final String PARAM_SEARCH_TERM = "searchTerm";
     public static final String PARAM_STATUS = "widgetStatus";
-    public static final String PARAM_TYPE = "type";
     public static final String PARAM_URL = "url";
 
     public static final String WIDGET_GET_ALL = "Widget.getAll";
@@ -84,22 +78,16 @@ public class Widget implements BasicEnti
     public static final String WIDGET_COUNT_BY_FREE_TEXT = "Widget.countByFreeText";
     public static final String WIDGET_GET_BY_STATUS = "Widget.getByStatus";
     public static final String WIDGET_COUNT_BY_STATUS = "Widget.countByStatus";
-    public static final String WIDGET_GET_BY_STATUS_AND_TYPE_AND_FREE_TEXT = "Widget.getByStatusAndTypeAndFreeText";
-    public static final String WIDGET_COUNT_BY_STATUS_AND_TYPE_AND_FREE_TEXT = "Widget.countByStatusAndTypeAndFreeText";
     public static final String WIDGET_GET_BY_URL = "Widget.getByUrl";
 
     static final String SELECT_W_FROM_WIDGET_W = "SELECT w FROM Widget w ";
     static final String SELECT_COUNT_W_FROM_WIDGET_W = "SELECT count(w) FROM Widget w ";
 
-    static final String WHERE_CLAUSE_STATUS_TYPE_FREE_TEXT =
-            " WHERE (:widgetStatus is null OR :widgetStatus = '' or w.widgetStatus = :widgetStatus)"
+
-                    " AND (:type is null OR :type = '' OR w.type = :type)" +
-                    " AND (:searchTerm is null OR :searchTerm = '' " +
-                    " OR lower(w.title) LIKE :searchTerm OR w.description LIKE :description)";
     static final String WHERE_CLAUSE_FREE_TEXT =
             " WHERE lower(w.title) LIKE :searchTerm OR w.description LIKE :description";
     static final String WHERE_CLAUSE_STATUS = " WHERE w.widgetStatus = :widgetStatus";
     static final String WHERE_CLAUSE_URL = " WHERE w.url = :url";
+    static final String ORDER_BY_TITLE_ASC = " ORDER BY w.title ASC ";
 
     @Id @Column(name="entity_id")
     @GeneratedValue(strategy = GenerationType.TABLE, generator = "widgetIdGenerator")

Modified: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/WidgetRepository.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/WidgetRepository.java?rev=1189099&r1=1189098&r2=1189099&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/WidgetRepository.java
(original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/WidgetRepository.java
Wed Oct 26 09:54:53 2011
@@ -87,13 +87,14 @@ public interface WidgetRepository extend
      * Gets a List of {@link Widget}'s by performing a free text search filtering on status
      *
      * @param widgetStatus status of the widget (PREVIEW, PUBLISHED etc)
+     * @param type         type of Widget (e.g. W3C, OpenSocial)
      * @param searchTerm   free text search term
      * @param offset       start point within the resultset (for paging)
      * @param pageSize     maximum number of items to be returned (for paging)
      * @return valid list of widgets, can be empty
      */
     List<Widget> getByStatusAndTypeAndFreeTextSearch(WidgetStatus widgetStatus, String
type, String searchTerm,
-                                              int offset, int pageSize);
+                                                     int offset, int pageSize);
 
 
     /**
@@ -101,6 +102,7 @@ public interface WidgetRepository extend
      * on status. Useful for paging.
      *
      * @param widgetStatus status of the widget (PREVIEW, PUBLISHED etc)
+     * @param type         type of Widget (e.g. W3C, OpenSocial)
      * @param searchTerm   free text search term
      * @return total number of {@link Widget}'s that match the search criteria
      */

Modified: incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaWidgetRepository.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaWidgetRepository.java?rev=1189099&r1=1189098&r2=1189099&view=diff
==============================================================================
--- incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaWidgetRepository.java
(original)
+++ incubator/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/repository/impl/JpaWidgetRepository.java
Wed Oct 26 09:54:53 2011
@@ -31,6 +31,13 @@ import org.springframework.stereotype.Re
 
 import javax.persistence.Query;
 import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Order;
+import javax.persistence.criteria.Path;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+import java.util.ArrayList;
 import java.util.List;
 
 import static org.apache.rave.persistence.jpa.util.JpaUtil.getPagedResultList;
@@ -100,24 +107,70 @@ public class JpaWidgetRepository extends
     @Override
     public List<Widget> getByStatusAndTypeAndFreeTextSearch(WidgetStatus widgetStatus,
String type, String searchTerm,
                                                             int offset, int pageSize) {
-        TypedQuery<Widget> query = manager.createNamedQuery(Widget.WIDGET_GET_BY_STATUS_AND_TYPE_AND_FREE_TEXT,
-                Widget.class);
-        query.setParameter(Widget.PARAM_STATUS, widgetStatus);
-        query.setParameter(Widget.PARAM_TYPE, type);
-        setFreeTextSearchTerm(query, searchTerm);
-        return getPagedResultList(query, offset, pageSize);
+        final CriteriaBuilder cb = manager.getCriteriaBuilder();
+        final CriteriaQuery<Widget> query = cb.createQuery(Widget.class);
+        final Root<Widget> widgetType = query.from(Widget.class);
+        query.where(getStatusAndTypeAndFreeTextPredicates(cb, widgetType, widgetStatus, type,
searchTerm));
+        query.orderBy(getOrderByTitleAsc(cb, widgetType));
+
+        return getPagedResultList(manager.createQuery(query), offset, pageSize);
     }
 
     @Override
     public int getCountByStatusAndTypeAndFreeText(WidgetStatus widgetStatus, String type,
String searchTerm) {
-        Query query = manager.createNamedQuery(Widget.WIDGET_COUNT_BY_STATUS_AND_TYPE_AND_FREE_TEXT);
-        query.setParameter(Widget.PARAM_STATUS, widgetStatus);
-        query.setParameter(Widget.PARAM_TYPE, type);
-        setFreeTextSearchTerm(query, searchTerm);
-        Number countResult = (Number) query.getSingleResult();
+        final CriteriaBuilder cb = manager.getCriteriaBuilder();
+        final CriteriaQuery<Long> query = cb.createQuery(Long.class);
+        final Root<Widget> widgetType = query.from(Widget.class);
+        query.select(cb.count(widgetType));
+        query.where(getStatusAndTypeAndFreeTextPredicates(cb, widgetType, widgetStatus, type,
searchTerm));
+
+        final Long countResult = manager.createQuery(query).getSingleResult();
         return countResult.intValue();
     }
 
+    private Predicate[] getStatusAndTypeAndFreeTextPredicates(CriteriaBuilder cb, Root<Widget>
widgetType,
+                                                              WidgetStatus widgetStatus,
String type,
+                                                              String searchTerm) {
+        List<Predicate> predicates = new ArrayList<Predicate>();
+        if (StringUtils.isNotBlank(searchTerm)) {
+            predicates.add(
+                    cb.or(
+                            cb.like(cb.lower(getTitleField(widgetType)), getLowercaseWildcardSearchTerm(searchTerm)),
+                            cb.like(cb.lower(getDescriptionField(widgetType)), getLowercaseWildcardSearchTerm(searchTerm))
+                    )
+            );
+        }
+        if (StringUtils.isNotBlank(type)) {
+            predicates.add(cb.and(cb.equal(getTypeField(widgetType), type)));
+        }
+        if (widgetStatus != null) {
+            predicates.add(cb.and(cb.equal(getWidgetStatusField(widgetType), widgetStatus)));
+        }
+
+        return predicates.toArray(new Predicate[predicates.size()]);
+    }
+
+    private Order getOrderByTitleAsc(CriteriaBuilder cb, Root<Widget> widgetType) {
+        return cb.asc(getTitleField(widgetType));
+    }
+
+    private Path<String> getTitleField(Root<Widget> widgetType) {
+        return widgetType.get("title");
+    }
+
+    private Path<String> getDescriptionField(Root<Widget> widgetType) {
+        return widgetType.get("description");
+    }
+
+    private Path<String> getTypeField(Root<Widget> widgetType) {
+        return widgetType.get("type");
+    }
+
+    private Path<WidgetStatus> getWidgetStatusField(Root<Widget> widgetType)
{
+        return widgetType.get("widgetStatus");
+    }
+
+
     @Override
     public Widget getByUrl(String widgetUrl) {
         if (StringUtils.isBlank(widgetUrl)) {
@@ -138,6 +191,13 @@ public class JpaWidgetRepository extends
      * @param searchTerm free text
      */
     protected void setFreeTextSearchTerm(Query query, final String searchTerm) {
-        query.setParameter(Widget.PARAM_SEARCH_TERM, "%" + searchTerm.toLowerCase() + "%");
+        query.setParameter(Widget.PARAM_SEARCH_TERM, getLowercaseWildcardSearchTerm(searchTerm));
+    }
+    
+    private String getLowercaseWildcardSearchTerm(String searchTerm) {
+        if (StringUtils.isBlank(searchTerm)) {
+            return searchTerm;
+        }
+        return "%" + searchTerm.toLowerCase() + "%";
     }
 }



Mime
View raw message