Author: andrei_exadel
Date: 2009-03-03 07:46:20 -0500 (Tue, 03 Mar 2009)
New Revision: 12798
Added:
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/utils/
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/utils/SearhUtils.java
Modified:
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/service/ISearchAction.java
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/service/SearchAction.java
Log:
Searching
Modified:
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/service/ISearchAction.java
===================================================================
---
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/service/ISearchAction.java 2009-03-03
12:46:14 UTC (rev 12797)
+++
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/service/ISearchAction.java 2009-03-03
12:46:20 UTC (rev 12798)
@@ -1,18 +1,16 @@
package org.richfaces.realworld.service;
-import java.util.Date;
import java.util.List;
-import java.util.Map;
-import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Image;
public interface ISearchAction {
- public List<Image> searchByImage(String name, String description, String path,
Integer width, Integer height, Integer size, Date createdDate, Date uploadedDate);
+ public List<Image> searchByImage(String query, boolean searchInMyAlbums, boolean
searchInShared);
- public List<Image> searchByTags(Map<String, String> params);
+ public List<Image> searchByTags(String query, boolean searchInMyAlbums, boolean
searchInShared);
- public List<Album> searchByAlbum(String name, String description, String
ownerName, boolean shared );
+ public List<Image> searchByAlbum(String query, boolean searchInMyAlbums, boolean
searchInShared);
- public List<Image> searchByUsers(Map<String, String> params);
+ public List<Image> searchByUsers(String query, boolean searchInMyAlbums, boolean
searchInShared);
+
}
Modified:
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/service/SearchAction.java
===================================================================
---
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/service/SearchAction.java 2009-03-03
12:46:14 UTC (rev 12797)
+++
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/service/SearchAction.java 2009-03-03
12:46:20 UTC (rev 12798)
@@ -1,8 +1,6 @@
package org.richfaces.realworld.service;
-import java.util.Date;
import java.util.List;
-import java.util.Map;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
@@ -11,12 +9,12 @@
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
-import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.User;
@Name("searchAction")
@Stateless
+@SuppressWarnings("unchecked")
public class SearchAction implements ISearchAction {
@In(value="entityManager")
@@ -25,92 +23,68 @@
@In @Out
private User user;
- public List<Image> searchByImage(String name, String description, String path,
Integer width, Integer height, Integer size, Date createdDate, Date uploadedDate){
- StringBuffer b = new StringBuffer("from Image i where ");
- if (name != null && name.length() > 0) {
- b.append("i.name like :name");
+ public List<Image> searchByAlbum(String searchQuery, boolean searchInMyAlbums,
boolean searchInShared) {
+ StringBuilder b = new StringBuilder("from Album a where a.name=:name and
a.shared=:shared");
+ if (searchInMyAlbums) {
+ b.append(" and a.owner.login=:login");
}
- if (description != null && description.length() > 0) {
- b.append("i.description like :description");
+ Query query = em.createQuery(b.toString());
+
+ query.setParameter("name", searchQuery);
+ query.setParameter("shared", searchInShared);
+
+ if (searchInMyAlbums) {
+ query.setParameter("login", user.getLogin());
}
- if (path != null && path.length() > 0) {
- b.append("i.path like :path");
+ return query.getResultList();
+ }
+
+ public List<Image> searchByImage(String searchQuery, boolean searchInMyAlbums,
boolean searchInShared) {
+ StringBuilder b = new StringBuilder("from Image i where i.name=:name and
i.album.shared=:shared");
+ if (searchInMyAlbums) {
+ b.append(" and i.album.owner.login=:login");
}
- if (width != null && width > 0) {
- b.append("i.width =:width");
- }
- if (height != null && height > 0) {
- b.append("i.height =:height)");
- }
- if (size != null && size > 0) {
- b.append("i.size =:size");
- }
- if (createdDate != null) {
- b.append("i.createdDate =:createdDate");
- }
- if (uploadedDate != null) {
- b.append("i.uploadedDate =:uploadedDate");
- }
+ Query query = em.createQuery(b.toString());
+ query.setParameter("name", searchQuery);
+ query.setParameter("shared", searchInShared);
- Query q = em.createQuery(b.toString());
-
- if (name != null && name.length() > 0) {
- q.setParameter("name", name);
+ if (searchInMyAlbums) {
+ query.setParameter("login", user.getLogin());
}
- if (description != null && description.length() > 0) {
- q.setParameter("description", description);
+ return query.getResultList();
+ }
+
+ public List<Image> searchByUsers(String searchQuery, boolean searchInMyAlbums,
boolean searchInShared) {
+ StringBuilder b = new StringBuilder("from User u where u.name=:name");
+ if (searchInShared) {
+ b.append(" and u.childAlbums.shared=true");
}
- if (path != null && path.length() > 0) {
- q.setParameter("path", path);
- }
- if (width != null && width > 0) {
- q.setParameter("width", width);
- }
- if (height != null && height > 0) {
- q.setParameter("height", height);
- }
- if (size != null && size > 0) {
- q.setParameter("size", size);
- }
- if (createdDate != null) {
- q.setParameter("createdDate", createdDate);
- }
- if (uploadedDate != null) {
- q.setParameter("uploadedDate", uploadedDate);
- }
+ Query query = em.createQuery(b.toString());
- return q.getResultList();
+ query.setParameter("name", searchQuery);
+
+ return query.getResultList();
}
-
- public List<Image> searchByTags(Map<String, String> params){
- return null;
- }
-
- public List<Album> searchByAlbum(String name, String description, String
ownerName, boolean shared ){
- StringBuffer b = new StringBuffer("from Album a where a.name like :name and
a.shared=:shared");
- if (description != null && description.length() > 0) {
- b.append("and a.description like :description ");
+
+ public List<Image> searchByTags(String searchQuery, boolean searchInMyAlbums,
boolean searchInShared) {
+ StringBuilder b = new StringBuilder("from MetaTag t where t.tag=:name");
+ if (searchInShared) {
+ b.append(" and t.albums.shared=true");
}
- if (ownerName != null && ownerName.length() > 0) {
- b.append("and a.owner.login like :ownewName");
+ if (searchInMyAlbums) {
+ b.append(" and t.albums.owner.login=:login");
}
- Query q = em.createQuery(b.toString())
- .setParameter("name", name)
- .setParameter("shared", shared);
- if (description != null && description.length() > 0) {
- q.setParameter("description", description);
- }
+ Query query = em.createQuery(b.toString());
- if (ownerName != null && ownerName.length() > 0) {
- q.setParameter("ownewName", ownerName);
+ query.setParameter("name", searchQuery);
+ if (searchInMyAlbums) {
+ query.setParameter("login", user.getLogin());
}
- return q.getResultList();
+ return query.getResultList();
}
+
- public List<Image> searchByUsers(Map<String, String> params){
- return null;
- }
}
Added:
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/utils/SearhUtils.java
===================================================================
---
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/utils/SearhUtils.java
(rev 0)
+++
trunk/test-applications/realworld2/ejb/src/main/java/org/richfaces/realworld/utils/SearhUtils.java 2009-03-03
12:46:20 UTC (rev 12798)
@@ -0,0 +1,69 @@
+/**
+ *
+ */
+package org.richfaces.realworld.utils;
+
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+/**
+ * @author Andrey Markavtsov
+ *
+ */
+public class SearhUtils {
+
+ static final String ALIAS = "a";
+
+ public static Query buildQuery (EntityManager em, String base, Map<String, Object>
params) {
+ StringBuilder b = new StringBuilder(base);
+ b.append(" ").append(ALIAS);
+ String [] properties = params.keySet().toArray(new String [] {});
+
+ if (properties.length > 0) {
+ b.append(" where ");
+ }
+
+ boolean isFirst = true;
+ for (int i=0; i < properties.length; i++) {
+ String property = properties[i];
+ if (propertyShouldBeSearched(params.get(property))) {
+ if (isFirst) {
+ isFirst = false;
+ }else {
+ b.append(" and ");
+ }
+ b.append(" a.").append(property).append("=");
+ b.append(":").append(property.replace(".", ""));
+
+ }
+ }
+
+ Query query = em.createQuery(b.toString());
+
+ for (int i=0; i < properties.length; i++) {
+ String property = properties[i];
+ if (propertyShouldBeSearched(params.get(property))) {
+ query.setParameter(property.replace(".", ""),
params.get(property));
+ }
+ }
+
+ return query;
+
+ }
+
+ private static boolean propertyShouldBeSearched(Object property) {
+ if (property == null) {
+ return false;
+ }
+ if (property instanceof String) {
+ return property.toString().length() > 0;
+ }else if (property instanceof Number) {
+ return ((Number)property).intValue() > 0;
+ }
+
+ return true;
+ }
+
+}