[jboss-svn-commits] JBL Code SVN: r5043 - in labs/shotoku/trunk: shotoku-base shotoku-base/src/java/org/jboss/shotoku/tools shotoku-feeds/src/java/org/jboss/shotoku/feeds/data shotoku-tags/src/java/org/jboss/shotoku/tags shotoku-tags/src/java/org/jboss/shotoku/tags/dal shotoku-tags/src/java/org/jboss/shotoku/tags/service shotoku-tags/src/java/org/jboss/shotoku/tags/test
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Jul 12 11:05:30 EDT 2006
Author: adamw
Date: 2006-07-12 11:05:24 -0400 (Wed, 12 Jul 2006)
New Revision: 5043
Added:
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/dal/CountersEntity.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/dal/CountersIdEntity.java
Modified:
labs/shotoku/trunk/shotoku-base/shotoku-base.iml
labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/TagFeed.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/ShotokuTag.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/TagService.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/WebsiteTag.java
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/test/TagsTestServlet.java
Log:
http://jira.jboss.org/jira/browse/JBSHOTOKU-86
Modified: labs/shotoku/trunk/shotoku-base/shotoku-base.iml
===================================================================
--- labs/shotoku/trunk/shotoku-base/shotoku-base.iml 2006-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-base/shotoku-base.iml 2006-07-12 15:05:24 UTC (rev 5043)
@@ -10,6 +10,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jboss" level="application" />
+ <orderEntry type="library" name="velocity" level="application" />
<orderEntryProperties />
</component>
</module>
Modified: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java 2006-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/tools/Tools.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -242,6 +242,18 @@
}
/**
+ * Checks if two objects are equal - either both null, or
+ * their equals method returns true.
+ * @param obj1 First object to compare.
+ * @param obj2 Second object to compare.
+ * @return True iff both objects are equal or if their equals
+ * method returns true.
+ */
+ public static boolean objectsEqual(Object obj1, Object obj2) {
+ return (obj1 == null && obj2 == null) || obj1.equals(obj2);
+ }
+
+ /**
* Transferes all bytes from the given input stream to the given output
* stream.
*
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/TagFeed.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/TagFeed.java 2006-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/TagFeed.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -3,6 +3,7 @@
import org.apache.velocity.VelocityContext;
import org.jboss.shotoku.ContentManager;
import org.jboss.shotoku.tags.Tag;
+import org.jboss.shotoku.tags.TagService;
import org.jboss.shotoku.tags.tools.TagTools;
import org.jboss.shotoku.tags.tools.Constants;
import org.jboss.shotoku.tags.tools.FeedType;
@@ -12,7 +13,6 @@
import java.io.OutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
-import java.io.StringWriter;
import java.util.*;
import java.net.URLDecoder;
@@ -79,6 +79,7 @@
String template = attributes.get("template");
List<Tag> tags;
+ TagService service = TagTools.getService();
if (FeedType.RESOURCE.toString().equals(infoTokens[0])) {
vc.put(FeedType.RESOURCE.toString(), infoDataDecoded);
@@ -93,6 +94,7 @@
template = template.replace(Constants.TAG_FEED_TYPE_VARIABLE,
FeedType.RESOURCE.toString());
+ service.increaseFeedCount(FeedType.RESOURCE, infoDataDecoded);
} else if (FeedType.AUTHOR.toString().equals(infoTokens[0])) {
vc.put(FeedType.AUTHOR.toString(), infoDataDecoded);
@@ -106,6 +108,7 @@
template = template.replace(Constants.TAG_FEED_TYPE_VARIABLE,
FeedType.AUTHOR.toString());
+ service.increaseFeedCount(FeedType.AUTHOR, infoDataDecoded);
} else if (FeedType.TAGS.toString().equals(infoTokens[0])) {
String[] tagNames = infoDataDecoded.split("[+]");
@@ -120,8 +123,7 @@
// Getting the tags with the given names.
try {
- tags = TagTools.getService().getTags(
- Arrays.asList(tagNames));
+ tags = service.getTags(Arrays.asList(tagNames));
} catch (TagGetException e) {
e.printStackTrace();
throw new IOException(e.getMessage());
@@ -130,6 +132,9 @@
template = template.replace(Constants.TAG_FEED_TYPE_VARIABLE,
FeedType.TAGS.toString());
+ for (String tagName : tagNames) {
+ service.increaseFeedCount(FeedType.TAGS, tagName);
+ }
} else {
throw new IOException("Invalid tag feed request.");
}
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-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -1,6 +1,8 @@
package org.jboss.shotoku.tags;
import org.jboss.shotoku.tags.dal.TagEntity;
+import org.jboss.shotoku.tags.tools.TagTools;
+import org.jboss.shotoku.tags.tools.FeedType;
import java.util.Date;
@@ -51,7 +53,7 @@
return dateCreated;
}
-public boolean equals(Object obj) {
+ public boolean equals(Object obj) {
if (obj == null) {
return false;
}
@@ -68,4 +70,9 @@
}
return false;
- }}
+ }
+
+ public Integer getFeedCounter() {
+ return TagTools.getService().getFeedCount(FeedType.TAGS, getName());
+ }
+}
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/ShotokuTag.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/ShotokuTag.java 2006-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/ShotokuTag.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -16,9 +16,4 @@
public String getType() {
return Constants.SHOTOKU_TAG;
}
-
- public Integer getFeedCounter() {
- // TODO Auto-generated method stub - dummy method
- return 1;
- }
}
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/TagService.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/TagService.java 2006-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/TagService.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -59,4 +59,22 @@
* @return List of matching tags
*/
public List<Tag> searchTags(String tag, String author, String keyword);
+
+ /**
+ * Gets count of number of times a feed of the given type and data
+ * was opened. Data for feed types is: author name, resource id or
+ * tag name.
+ * @param feedType Type of the feed to which the data corresponds to.
+ * @param data Data of the feed for which the counter is read.
+ * @return Number of times a given feed was opened.
+ */
+ public int getFeedCount(FeedType feedType, String data);
+
+ /**
+ * Increases the number of times a feed of the given type and data
+ * was opened.
+ * @param feedType Type of the feed for which to increase the counter.
+ * @param data Data of the feed for which to increase the counter.
+ */
+ public void increaseFeedCount(FeedType feedType, String data);
}
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/WebsiteTag.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/WebsiteTag.java 2006-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/WebsiteTag.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -16,9 +16,4 @@
public String getType() {
return Constants.WEBSITE_TAG;
}
-
- public Integer getFeedCounter() {
- // TODO Auto-generated method stub - dummy method
- return 1;
- }
}
Added: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/dal/CountersEntity.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/dal/CountersEntity.java 2006-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/dal/CountersEntity.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -0,0 +1,49 @@
+package org.jboss.shotoku.tags.dal;
+
+import javax.persistence.*;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+ at Entity
+ at Table(name = "ShotokuTagsCounters")
+public class CountersEntity {
+ private CountersIdEntity id;
+ private int count;
+
+ public CountersEntity() {
+
+ }
+
+ public CountersEntity(CountersIdEntity id, int count) {
+ this.id = id;
+ this.count = count;
+ }
+
+ @EmbeddedId
+ public CountersIdEntity getId() {
+ return id;
+ }
+
+ public void setId(CountersIdEntity id) {
+ this.id = id;
+ }
+
+ @Transient
+ public String getType() {
+ return id.getType();
+ }
+
+ @Transient
+ public String getData() {
+ return id.getData();
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ public void setCount(int count) {
+ this.count = count;
+ }
+}
Added: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/dal/CountersIdEntity.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/dal/CountersIdEntity.java 2006-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/dal/CountersIdEntity.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -0,0 +1,55 @@
+package org.jboss.shotoku.tags.dal;
+
+import org.jboss.shotoku.tools.Tools;
+
+import javax.persistence.Embeddable;
+import javax.persistence.Column;
+import java.io.Serializable;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+ at Embeddable
+public class CountersIdEntity implements Serializable {
+ private String data;
+ private String type;
+
+ public CountersIdEntity() {
+
+ }
+
+ public CountersIdEntity(String data, String type) {
+ this.data = data;
+ this.type = type;
+ }
+
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
+ @Column(length = 32)
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public int hashCode() {
+ return (data == null ? 0 : data.hashCode()) +
+ (type == null ? 0 : type.hashCode());
+ }
+
+ public boolean equals(Object obj) {
+ if (obj == this) return true;
+ if (!(obj instanceof CountersIdEntity)) return false;
+ CountersIdEntity cie = (CountersIdEntity) obj;
+ return Tools.objectsEqual(type, cie.type) &&
+ Tools.objectsEqual(data, cie.data);
+ }
+}
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-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -51,6 +51,8 @@
import org.jboss.shotoku.tags.exceptions.TagAddException;
import org.jboss.shotoku.tags.exceptions.TagDeleteException;
import org.jboss.shotoku.tags.dal.TagEntity;
+import org.jboss.shotoku.tags.dal.CountersIdEntity;
+import org.jboss.shotoku.tags.dal.CountersEntity;
/**
* @author Adam Warski (adamw at aster.pl)
@@ -61,342 +63,362 @@
@Management(TagService.class)
@Depends(Constants.SHOTOKU_SERVICE_NAME)
public class TagServiceImpl extends AdministratedServiceImpl implements
- TagService, TagServiceLocal {
- private static final Logger log = Logger.getLogger(AdministratedService.class);
+ TagService, TagServiceLocal {
+ private static final Logger log = Logger.getLogger(AdministratedService.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 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;
- /**
- * MIN_SIMILARITY - minimal similarity for checking relatedTags needed
- */
- private double MIN_SIMILARITY = 0.7d;
+ /**
+ * MIN_SIMILARITY - minimal similarity for checking relatedTags needed
+ */
+ private double MIN_SIMILARITY = 0.7d;
- private TagEntity getTagEntity(Tag t) {
- TagEntity te = new TagEntity();
+ private TagEntity getTagEntity(Tag t) {
+ TagEntity te = new TagEntity();
- te.setAuthor(t.getAuthor());
- te.setData(t.getData());
- te.setDateCreated(t.getDateCreated());
- te.setName(t.getName());
- te.setResourceId(t.getResourceId());
- te.setType(t.getType());
+ te.setAuthor(t.getAuthor());
+ te.setData(t.getData());
+ te.setDateCreated(t.getDateCreated());
+ te.setName(t.getName());
+ te.setResourceId(t.getResourceId());
+ te.setType(t.getType());
- return te;
- }
+ return te;
+ }
- public void addTag(Tag t) throws TagAddException {
- try {
- manager.persist(getTagEntity(t));
- } catch (Throwable e) {
- throw new TagAddException(e);
- }
- }
+ public void addTag(Tag t) throws TagAddException {
+ try {
+ manager.persist(getTagEntity(t));
+ } catch (Throwable e) {
+ throw new TagAddException(e);
+ }
+ }
- public void deleteTag(Tag t) throws TagDeleteException {
- try {
- manager.remove(getTagEntity(t));
- } catch (Throwable e) {
- throw new TagDeleteException(e);
- }
- }
+ public void deleteTag(Tag t) throws TagDeleteException {
+ try {
+ manager.remove(getTagEntity(t));
+ } catch (Throwable e) {
+ throw new TagDeleteException(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 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();
- List<Tag> ret = new ArrayList<Tag>();
- for (TagEntity te : result) {
- ret.add(te.getTag());
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- }
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ }
+ }
- public Tag getTag(String tagName) throws TagGetException {
- try {
- // noinspection unchecked
- TagEntity result = (TagEntity) manager.createQuery(
- "from TagEntity where name = :name").setParameter("name",
- tagName).getSingleResult();
+ public Tag getTag(String tagName) throws TagGetException {
+ try {
+ // noinspection unchecked
+ TagEntity result = (TagEntity) manager.createQuery(
+ "from TagEntity where name = :name").setParameter("name",
+ tagName).getSingleResult();
- if (result == null) {
- return null;
- }
+ if (result == null) {
+ return null;
+ }
- return result.getTag();
- } catch (Throwable e) {
- throw new TagGetException(e);
- }
- }
+ return result.getTag();
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ }
+ }
- 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);
+ 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);
- iter.next();
+ iter.next();
- if (iter.hasNext()) {
- querySb.append(" and ");
- }
+ if (iter.hasNext()) {
+ querySb.append(" and ");
+ }
- i++;
- }
+ i++;
+ }
- Query query = manager.createQuery(querySb.toString());
- i = 0;
- for (String tagName : tagNames) {
- query.setParameter("name" + i++, tagName);
- }
+ Query query = manager.createQuery(querySb.toString());
+ i = 0;
+ for (String tagName : tagNames) {
+ query.setParameter("name" + i++, tagName);
+ }
- // noinspection unchecked
- List<TagEntity> result = query.getResultList();
+ // noinspection unchecked
+ List<TagEntity> result = query.getResultList();
- List<Tag> ret = new ArrayList<Tag>();
- for (TagEntity te : result) {
- ret.add(te.getTag());
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- }
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ }
+ }
- public List<Tag> getRelatedTags(List<Tag> relateTo) throws TagGetException {
- List<Tag> ret = new ArrayList<Tag>();
+ public List<Tag> getRelatedTags(List<Tag> relateTo) throws TagGetException {
+ List<Tag> ret = new ArrayList<Tag>();
- Map<String, List<Tag>> otherResources = new HashMap<String, List<Tag>>();
+ Map<String, List<Tag>> otherResources = new HashMap<String, List<Tag>>();
- for (Tag relatedTag : relateTo) {
- List<TagEntity> result = manager.createQuery(
- "from TagEntity where name = :name").setParameter("name",
- relatedTag.getName()).getResultList();
+ for (Tag relatedTag : relateTo) {
+ List<TagEntity> result = manager.createQuery(
+ "from TagEntity where name = :name").setParameter("name",
+ relatedTag.getName()).getResultList();
- for (TagEntity otherTag : result) {
- if (!otherResources.containsKey(otherTag.getResourceId())) {
- otherResources.put(otherTag.getResourceId(),
- getTags(otherTag.getResourceId()));
- }
- }
- }
+ for (TagEntity otherTag : result) {
+ if (!otherResources.containsKey(otherTag.getResourceId())) {
+ otherResources.put(otherTag.getResourceId(),
+ getTags(otherTag.getResourceId()));
+ }
+ }
+ }
- for (List<Tag> tagList : otherResources.values()) {
- if (checkSimilarity(relateTo, tagList) >= MIN_SIMILARITY) {
- ret.addAll(tagList);
- }
- }
+ for (List<Tag> tagList : otherResources.values()) {
+ if (checkSimilarity(relateTo, tagList) >= MIN_SIMILARITY) {
+ ret.addAll(tagList);
+ }
+ }
- // don't return "relateTo" members
- List<Tag> endRet = new ArrayList<Tag>(ret);
- for (Tag tag : ret) {
- if (tagListContainsTag(tag, relateTo)) {
- endRet.remove(tag);
- }
- }
+ // 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;
- }
+ return endRet;
+ }
- private boolean tagListContainsTag(Tag tag, List<Tag> listToCheck) {
- for (Tag tag2 : listToCheck) {
- if (tag.getName().equals(tag2.getName())) {
- return true;
- }
- }
+ private boolean tagListContainsTag(Tag tag, List<Tag> listToCheck) {
+ for (Tag tag2 : listToCheck) {
+ if (tag.getName().equals(tag2.getName())) {
+ return true;
+ }
+ }
- return false;
- }
+ return false;
+ }
- private double checkSimilarity(List<Tag> givenTags, List<Tag> listToCheck) {
- double ret = 0;
+ private double checkSimilarity(List<Tag> givenTags, List<Tag> listToCheck) {
+ double ret = 0;
- for (Tag tag : givenTags) {
- if (tagListContainsTag(tag, listToCheck)) {
- ret++;
- }
- }
+ for (Tag tag : givenTags) {
+ if (tagListContainsTag(tag, listToCheck)) {
+ ret++;
+ }
+ }
- return ret / (double) givenTags.size();
- }
+ 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();
+ 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());
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- }
- }
+ 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, 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();
+ 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("+");
- }
- }
+ 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 "";
- }
- }
+ try {
+ return "/feeds/tag/" + feedType.toString() + "/"
+ + URLEncoder.encode(sb.toString(), "UTF-8") + "/" + type;
+ } catch (UnsupportedEncodingException e) {
+ return "";
+ }
+ }
- public List<String> getAllTagsNames() throws TagGetException {
- List<String> result = manager.createQuery("SELECT DISTINCT name FROM TagEntity")
- .getResultList();
-
- return result;
- }
-
- public List<Tag> getAllTags() throws TagGetException {
- List<Tag> result = new ArrayList<Tag>();
- List<TagEntity> tagEnitites = manager.createQuery("from TagEntity")
- .getResultList();
-
- for (TagEntity tagEnt : tagEnitites) {
- result.add(tagEnt.getTag());
- }
-
- return result;
- }
+ public List<String> getAllTagsNames() throws TagGetException {
+ List<String> result = manager.createQuery("SELECT DISTINCT name FROM TagEntity")
+ .getResultList();
- public List<Tag> searchTags(String tag, String author, String keyword) {
- List<Tag> searchResult = new ArrayList<Tag>();
-
- log.info("'" + tag + "' '" + author + "' '" + keyword + "'");
-
- TagSearch tagSearch = new TagSearch(tag, author, keyword);
-
- try {
- List<Tag> allTags = getAllTags();
-
- for (Tag nextTag : allTags) {
- if (tagSearch.matches(nextTag)) {
- searchResult.add(nextTag);
- }
- }
- } catch (TagGetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- return searchResult;
- }
+ return result;
+ }
+
+ public List<Tag> getAllTags() throws TagGetException {
+ List<Tag> result = new ArrayList<Tag>();
+ List<TagEntity> tagEnitites = manager.createQuery("from TagEntity")
+ .getResultList();
+
+ for (TagEntity tagEnt : tagEnitites) {
+ result.add(tagEnt.getTag());
+ }
+
+ return result;
+ }
+
+ public List<Tag> searchTags(String tag, String author, String keyword) {
+ List<Tag> searchResult = new ArrayList<Tag>();
+
+ log.info("'" + tag + "' '" + author + "' '" + keyword + "'");
+
+ TagSearch tagSearch = new TagSearch(tag, author, keyword);
+
+ try {
+ List<Tag> allTags = getAllTags();
+
+ for (Tag nextTag : allTags) {
+ if (tagSearch.matches(nextTag)) {
+ searchResult.add(nextTag);
+ }
+ }
+ } catch (TagGetException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return searchResult;
+ }
+
+ public int getFeedCount(FeedType feedType, String data) {
+ CountersEntity ce = manager.find(CountersEntity.class,
+ new CountersIdEntity(data, feedType.toString()));
+
+ return ce == null ? 0 : ce.getCount();
+ }
+
+ public void increaseFeedCount(FeedType feedType, String data) {
+ CountersEntity ce = manager.find(CountersEntity.class,
+ new CountersIdEntity(data, feedType.toString()));
+
+ if (ce == null) {
+ manager.persist(new CountersEntity(
+ new CountersIdEntity(data, feedType.toString()), 1));
+ } else {
+ ce.setCount(ce.getCount()+1);
+ manager.flush();
+ }
+ }
}
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsTestServlet.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsTestServlet.java 2006-07-12 14:52:00 UTC (rev 5042)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsTestServlet.java 2006-07-12 15:05:24 UTC (rev 5043)
@@ -1,6 +1,7 @@
package org.jboss.shotoku.tags.test;
import org.jboss.shotoku.tags.tools.TagTools;
+import org.jboss.shotoku.tags.tools.FeedType;
import org.jboss.shotoku.tags.ShotokuTag;
import org.jboss.shotoku.tags.Tag;
import org.jboss.shotoku.tags.exceptions.TagAddException;
@@ -22,7 +23,7 @@
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
httpServletResponse.setContentType("text/html");
- httpServletResponse.getWriter().println(TagTools.getService().getServiceName());
+ /*httpServletResponse.getWriter().println(TagTools.getService().getServiceName());
try {
TagTools.getService().addTag(new ShotokuTag("n" + i++, "a1", "r1", "d1",
@@ -37,6 +38,12 @@
}
} catch (TagGetException e) {
e.printStackTrace();
- }
+ } */
+
+ httpServletResponse.getWriter().println(
+ TagTools.getService().getFeedCount(FeedType.AUTHOR, "adamw"));
+ TagTools.getService().increaseFeedCount(FeedType.AUTHOR, "adamw");
+ httpServletResponse.getWriter().println(
+ TagTools.getService().getFeedCount(FeedType.AUTHOR, "adamw"));
}
}
More information about the jboss-svn-commits
mailing list