|
From: <jbo...@li...> - 2006-06-10 14:25:32
|
Author: szimano
Date: 2006-06-10 10:25:28 -0400 (Sat, 10 Jun 2006)
New Revision: 4719
Modified:
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java
Log:
getRelatedTags() + AbstractTag equals() methos
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java 2006-06-10 04:18:23 UTC (rev 4718)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java 2006-06-10 14:25:28 UTC (rev 4719)
@@ -8,43 +8,64 @@
* @author Adam Warski (ad...@as...)
*/
public abstract class AbstractTag implements Tag {
- private String name;
- private String author;
- private String resourceId;
- private String data;
+ private String name;
- private Date dateCreated;
+ private String author;
- public AbstractTag(String name, String author, String resourceId,
- String data, Date dateCreated) {
- this.name = name;
- this.author = author;
- this.resourceId = resourceId;
- this.data = data;
- this.dateCreated = dateCreated;
- }
+ private String resourceId;
- /*
- * TAG implementation.
- */
+ private String data;
- public String getName() {
- return name;
- }
+ private Date dateCreated;
- public String getAuthor() {
- return author;
- }
+ public AbstractTag(String name, String author, String resourceId,
+ String data, Date dateCreated) {
+ this.name = name;
+ this.author = author;
+ this.resourceId = resourceId;
+ this.data = data;
+ this.dateCreated = dateCreated;
+ }
- public String getResourceId() {
- return resourceId;
- }
+ /*
+ * TAG implementation.
+ */
- public String getData() {
- return data;
- }
+ public String getName() {
+ return name;
+ }
- public Date getDateCreated() {
- return dateCreated;
- }
-}
+ public String getAuthor() {
+ return author;
+ }
+
+ public String getResourceId() {
+ return resourceId;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public Date getDateCreated() {
+ return dateCreated;
+ }
+
+public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+
+ if (obj.getClass().equals(this.getClass())) {
+ AbstractTag otherTag = (AbstractTag) obj;
+
+ if (name.equals(otherTag.getName())
+ && ((data == null && otherTag.getData() == null) || (data != null && data.equals(otherTag.getData())))
+ && resourceId.equals(otherTag.getResourceId())
+ && author.equals(otherTag.getAuthor())) {
+ return true;
+ }
+ }
+
+ return false;
+ }}
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java 2006-06-10 04:18:23 UTC (rev 4718)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java 2006-06-10 14:25:28 UTC (rev 4719)
@@ -22,9 +22,11 @@
package org.jboss.shotoku.tags.service;
import java.util.Calendar;
+import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
+import java.util.Map;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
@@ -54,246 +56,310 @@
* @author Adam Warski (ad...@as...)
* @author Damon Sicore (da...@si...)
*/
-@Service(objectName=org.jboss.shotoku.tags.tools.Constants.TAG_SERVICE_NAME)
+@Service(objectName = org.jboss.shotoku.tags.tools.Constants.TAG_SERVICE_NAME)
@Local(TagServiceLocal.class)
@Management(TagService.class)
@Depends(Constants.SHOTOKU_SERVICE_NAME)
-public class TagServiceImpl extends AdministratedServiceImpl
- implements TagService, TagServiceLocal {
- private static final Logger log = Logger.getLogger(TagService.class);
+public class TagServiceImpl extends AdministratedServiceImpl implements
+ TagService, TagServiceLocal {
+ private static final Logger log = Logger.getLogger(TagService.class);
- /*
- * Service lifecycle management.
- */
+ /*
+ * Service lifecycle management.
+ */
- public void create() throws Exception {
- super.create();
+ public void create() throws Exception {
+ super.create();
- // Enabling administration for this service.
- Tools.getService().addAdministratedService(new AdministratedServiceGetter() {
- public AdministratedService getService() {
- return TagTools.getService();
- }
- });
+ // Enabling administration for this service.
+ Tools.getService().addAdministratedService(
+ new AdministratedServiceGetter() {
+ public AdministratedService getService() {
+ return TagTools.getService();
+ }
+ });
- setTimerInterval(10000);
+ setTimerInterval(10000);
- log.info("Tag service created.");
- }
+ log.info("Tag service created.");
+ }
- public void start() throws Exception {
- super.start();
+ public void start() throws Exception {
+ super.start();
- // Starting the updater thread.
- new Thread() {
- {
- setDaemon(true);
- }
+ // Starting the updater thread.
+ new Thread() {
+ {
+ setDaemon(true);
+ }
- public void run() {
- while (getServiceRunnable()) {
- try {
- sleep(getTimerInterval());
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
+ public void run() {
+ while (getServiceRunnable()) {
+ try {
+ sleep(getTimerInterval());
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
- try {
- update();
- } catch (Throwable t) {
- // Making sure that an exception won't stop the thread.
- }
+ try {
+ update();
+ } catch (Throwable t) {
+ // Making sure that an exception won't stop the thread.
+ }
- setLastUpdate(Calendar.getInstance().getTimeInMillis());
- }
+ setLastUpdate(Calendar.getInstance().getTimeInMillis());
+ }
- log.info("Tag service deaemon thread terminated.");
- }
- }.start();
+ log.info("Tag service deaemon thread terminated.");
+ }
+ }.start();
- log.info("Tag service started with update interval: "+ getTimerInterval());
- }
+ log.info("Tag service started with update interval: "
+ + getTimerInterval());
+ }
- public void stop() {
- super.stop();
- log.info("Tag service signaled to stop.");
- }
+ public void stop() {
+ super.stop();
+ log.info("Tag service signaled to stop.");
+ }
- public void destroy() {
- }
+ public void destroy() {
+ }
- /*
- * Timer-handling functions.
- */
+ /*
+ * Timer-handling functions.
+ */
- public void update() {
+ public void update() {
- }
+ }
- public String getServiceId() {
- return "ShotokuTagService";
- }
+ public String getServiceId() {
+ return "ShotokuTagService";
+ }
- public String getServiceName() {
- return "Tag service";
- }
+ public String getServiceName() {
+ return "Tag service";
+ }
- public String getServiceDescription() {
- return "Shotoku tag service";
- }
+ public String getServiceDescription() {
+ return "Shotoku tag service";
+ }
- /*
- * TagService implementation.
- */
+ /*
+ * TagService implementation.
+ */
- @PersistenceContext
- private EntityManager manager;
+ @PersistenceContext
+ private EntityManager manager;
- private TagEntity getTagEntity(Tag t) {
- TagEntity te = new TagEntity();
+ /**
+ * MIN_SIMILARITY - minimal similarity for checking relatedTags needed
+ */
+ private double MIN_SIMILARITY = 0.7d;
- te.setAuthor(t.getAuthor());
- te.setData(t.getData());
- te.setDateCreated(t.getDateCreated());
- te.setName(t.getName());
- te.setResourceId(t.getResourceId());
- te.setType(t.getType());
+ private TagEntity getTagEntity(Tag t) {
+ TagEntity te = new TagEntity();
- return te;
- }
+ te.setAuthor(t.getAuthor());
+ te.setData(t.getData());
+ te.setDateCreated(t.getDateCreated());
+ te.setName(t.getName());
+ te.setResourceId(t.getResourceId());
+ te.setType(t.getType());
- public void addTag(Tag t) throws TagAddException {
- try {
- manager.persist(getTagEntity(t));
- } catch (Throwable e) {
- throw new TagAddException(e);
- }
- }
+ return te;
+ }
- public void deleteTag(Tag t) throws TagDeleteException {
- try {
- manager.remove(getTagEntity(t));
- } catch (Throwable e) {
- throw new TagDeleteException(e);
- }
- }
+ public void addTag(Tag t) throws TagAddException {
+ try {
+ manager.persist(getTagEntity(t));
+ } catch (Throwable e) {
+ throw new TagAddException(e);
+ }
+ }
- public List<Tag> getTags(String resourceId) throws TagGetException {
- try {
- //noinspection unchecked
- List<TagEntity> result = manager.createQuery(
- "from TagEntity where resourceId = :resourceId order by dateCreated desc").
- setParameter("resourceId", resourceId).getResultList();
+ public void deleteTag(Tag t) throws TagDeleteException {
+ try {
+ manager.remove(getTagEntity(t));
+ } catch (Throwable e) {
+ throw new TagDeleteException(e);
+ }
+ }
- List<Tag> ret = new ArrayList<Tag>();
- for (TagEntity te : result) {
- ret.add(te.getTag());
- }
+ public List<Tag> getTags(String resourceId) throws TagGetException {
+ try {
+ // noinspection unchecked
+ List<TagEntity> result = manager
+ .createQuery(
+ "from TagEntity where resourceId = :resourceId order by dateCreated desc")
+ .setParameter("resourceId", resourceId).getResultList();
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- }
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
- public Tag getTag(String tagName) throws TagGetException {
- try {
- //noinspection unchecked
- TagEntity result = (TagEntity) manager.createQuery("from TagEntity where name = :name").
- setParameter("name", tagName).getSingleResult();
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ }
+ }
- if (result == null) {
- return null;
- }
+ public Tag getTag(String tagName) throws TagGetException {
+ try {
+ // noinspection unchecked
+ TagEntity result = (TagEntity) manager.createQuery(
+ "from TagEntity where name = :name").setParameter("name",
+ tagName).getSingleResult();
- return result.getTag();
- } catch (Throwable e) {
- throw new TagGetException(e);
- }
- }
+ if (result == null) {
+ return null;
+ }
- public List<Tag> getTags(List<String> tagNames) throws TagGetException {
- try {
- StringBuffer querySb = new StringBuffer("from TagEntity where ");
- int i = 0;
- for (Iterator iter = tagNames.iterator(); iter.hasNext();) {
- querySb.append("name").append(" = :name").append(i);
+ return result.getTag();
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ }
+ }
- iter.next();
+ public List<Tag> getTags(List<String> tagNames) throws TagGetException {
+ try {
+ StringBuffer querySb = new StringBuffer("from TagEntity where ");
+ int i = 0;
+ for (Iterator iter = tagNames.iterator(); iter.hasNext();) {
+ querySb.append("name").append(" = :name").append(i);
- if (iter.hasNext()) {
- querySb.append(" and ");
- }
+ iter.next();
- i++;
- }
+ if (iter.hasNext()) {
+ querySb.append(" and ");
+ }
- Query query = manager.createQuery(querySb.toString());
- i = 0;
- for (String tagName : tagNames) {
- query.setParameter("name" + i++, tagName);
- }
+ i++;
+ }
- //noinspection unchecked
- List<TagEntity> result = query.getResultList();
+ Query query = manager.createQuery(querySb.toString());
+ i = 0;
+ for (String tagName : tagNames) {
+ query.setParameter("name" + i++, tagName);
+ }
- List<Tag> ret = new ArrayList<Tag>();
- for (TagEntity te : result) {
- ret.add(te.getTag());
- }
+ // noinspection unchecked
+ List<TagEntity> result = query.getResultList();
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- }
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
- public List<Tag> getRelatedTags(List<Tag> relateTo) throws TagGetException {
- return null;
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ }
+ }
- public List<Tag> getTagsByAuthor(String author) throws TagGetException {
- try {
- //noinspection unchecked
- List<TagEntity> result = manager.createQuery(
- "from TagEntity where author = :author order by dateCreated desc").
- setParameter("author", author).getResultList();
+ public List<Tag> getRelatedTags(List<Tag> relateTo) throws TagGetException {
+ List<Tag> ret = new ArrayList<Tag>();
- List<Tag> ret = new ArrayList<Tag>();
- for (TagEntity te : result) {
- ret.add(te.getTag());
- }
+ Map<String, List<Tag>> otherResources = new HashMap<String, List<Tag>>();
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- }
- }
+ for (Tag relatedTag : relateTo) {
+ List<TagEntity> result = manager.createQuery(
+ "from TagEntity where name = :name").setParameter("name",
+ relatedTag.getName()).getResultList();
- public String getFeedLink(FeedType feedType, String data, String type) {
- try {
- return "/feeds/tag/" + feedType.toString() + "/" +
- URLEncoder.encode(data, "UTF-8") + "/" + type;
- } catch (UnsupportedEncodingException e) {
- return "";
- }
- }
+ for (TagEntity otherTag : result) {
+ if (!otherResources.containsKey(otherTag.getResourceId())) {
+ otherResources.put(otherTag.getResourceId(),
+ getTags(otherTag.getResourceId()));
+ }
+ }
+ }
- public String getFeedLink(FeedType feedType, List<String> dataList, String type) {
- StringBuffer sb = new StringBuffer();
+ for (List<Tag> tagList : otherResources.values()) {
+ if (checkSimilarity(relateTo, tagList) >= MIN_SIMILARITY) {
+ ret.addAll(tagList);
+ }
+ }
- for (Iterator<String> iter = dataList.iterator(); iter.hasNext();) {
- sb.append(iter.next());
- if (iter.hasNext()) {
- sb.append("+");
- }
- }
+ // don't return "relateTo" members
+ List<Tag> endRet = new ArrayList<Tag>(ret);
+ for (Tag tag : ret) {
+ if (tagListContainsTag(tag, relateTo)) {
+ endRet.remove(tag);
+ }
+ }
+
+ return endRet;
+ }
+
+ private boolean tagListContainsTag(Tag tag, List<Tag> listToCheck) {
+ for (Tag tag2 : listToCheck) {
+ if (tag.getName().equals(tag2.getName())) {
+ return true;
+ }
+ }
+
+ return false;
+ }
- try {
- return "/feeds/tag/" + feedType.toString() + "/" +
- URLEncoder.encode(sb.toString(), "UTF-8") + "/" + type;
- } catch (UnsupportedEncodingException e) {
- return "";
- }
- }
+ private double checkSimilarity(List<Tag> givenTags, List<Tag> listToCheck) {
+ double ret = 0;
+
+ for (Tag tag : givenTags) {
+ if (tagListContainsTag(tag,listToCheck)) {
+ ret++;
+ }
+ }
+
+ return ret / (double) givenTags.size();
+ }
+
+ public List<Tag> getTagsByAuthor(String author) throws TagGetException {
+ try {
+ // noinspection unchecked
+ List<TagEntity> result = manager
+ .createQuery(
+ "from TagEntity where author = :author order by dateCreated desc")
+ .setParameter("author", author).getResultList();
+
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
+
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ }
+ }
+
+ public String getFeedLink(FeedType feedType, String data, String type) {
+ try {
+ return "/feeds/tag/" + feedType.toString() + "/"
+ + URLEncoder.encode(data, "UTF-8") + "/" + type;
+ } catch (UnsupportedEncodingException e) {
+ return "";
+ }
+ }
+
+ public String getFeedLink(FeedType feedType, List<String> dataList,
+ String type) {
+ StringBuffer sb = new StringBuffer();
+
+ for (Iterator<String> iter = dataList.iterator(); iter.hasNext();) {
+ sb.append(iter.next());
+ if (iter.hasNext()) {
+ sb.append("+");
+ }
+ }
+
+ try {
+ return "/feeds/tag/" + feedType.toString() + "/"
+ + URLEncoder.encode(sb.toString(), "UTF-8") + "/" + type;
+ } catch (UnsupportedEncodingException e) {
+ return "";
+ }
+ }
}
|