001 package org.deepamehta.plugins.review; 002 003 import de.deepamehta.core.Topic; 004 import de.deepamehta.core.TopicType; 005 import de.deepamehta.core.model.AssociationDefinitionModel; 006 import de.deepamehta.core.osgi.PluginActivator; 007 import de.deepamehta.core.service.ClientState; 008 import de.deepamehta.core.service.Directives; 009 import de.deepamehta.core.storage.spi.DeepaMehtaTransaction; 010 import java.util.Collection; 011 import java.util.logging.Logger; 012 import javax.ws.rs.*; 013 import org.deepamehta.plugins.review.service.ReviewService; 014 015 016 017 /** 018 * A very stupid plugin for counting likes/dislikes on any kind of topics in DeepaMehta. 019 * 020 * * Counting values seperately as "Good" and "So-so", depends on introducing Migration2 to your application model. 021 * * Counting values as one accumulate "Score", depends on introducing Migration1 to your application model. 022 * 023 * @author Malte Reißig (<malte@mikromedia.de>) 024 * @website https://github.com/mukil/org.deepamehta-reviews 025 * @version 0.3.6-SNAPSHOT 026 * 027 */ 028 029 @Path("/review") 030 @Consumes("application/json") 031 @Produces("application/json") 032 public class ReviewPlugin extends PluginActivator implements ReviewService { 033 034 private Logger log = Logger.getLogger(getClass().getName()); 035 036 public final static String SCORE_TYPE_URI = "org.deepamehta.reviews.score"; 037 public final static String GOOD_TYPE_URI = "org.deepamehta.reviews.good"; 038 public final static String SOSO_TYPE_URI = "org.deepamehta.reviews.soso"; 039 040 041 /** Increments the number of supportive voices (yelling "Good!"). */ 042 043 @GET 044 @Path("/good/{id}") 045 @Produces("application/json") 046 @Override 047 public Topic addToGood(@PathParam("id") long resourceId, @HeaderParam("Cookie") ClientState clientState) { 048 049 DeepaMehtaTransaction tx = dms.beginTx(); 050 Topic topic = null; 051 try { 052 // fixme: check if SCORE_URI is part of given topics type definition 053 topic = dms.getTopic(resourceId, true); 054 TopicType typeDef = dms.getTopicType(topic.getTypeUri()); 055 Collection<AssociationDefinitionModel> typeModel = typeDef.getModel().getAssocDefs(); 056 boolean hasScoreType = false; 057 for (AssociationDefinitionModel associationDefinitionModel : typeModel) { 058 if (associationDefinitionModel.getChildTypeUri().equals(GOOD_TYPE_URI)) hasScoreType = true; 059 } 060 if (hasScoreType) { 061 int score = topic.getCompositeValue().getModel().getInt(GOOD_TYPE_URI) + 1; 062 topic.getCompositeValue().set(GOOD_TYPE_URI, score, clientState, new Directives()); 063 tx.success(); 064 } else { 065 throw new WebApplicationException(new RuntimeException("The TypeDefinition (model) of the given topic " 066 + "does not contain the \"org.deepamehta.reviews.good\"-type")); 067 } 068 } catch (Exception e) { 069 throw new WebApplicationException(new RuntimeException("something went wrong", e)); 070 } finally { 071 tx.finish(); 072 } 073 return topic; 074 } 075 076 @GET 077 @Path("/soso/{id}") 078 @Produces("application/json") 079 @Override 080 public Topic addToSoso(@PathParam("id") long resourceId, @HeaderParam("Cookie") ClientState clientState) { 081 082 DeepaMehtaTransaction tx = dms.beginTx(); 083 Topic topic = null; 084 try { 085 // fixme: check if SCORE_URI is part of given topics type definition 086 topic = dms.getTopic(resourceId, true); 087 TopicType typeDef = dms.getTopicType(topic.getTypeUri()); 088 Collection<AssociationDefinitionModel> typeModel = typeDef.getModel().getAssocDefs(); 089 boolean hasScoreType = false; 090 for (AssociationDefinitionModel associationDefinitionModel : typeModel) { 091 if (associationDefinitionModel.getChildTypeUri().equals(SOSO_TYPE_URI)) hasScoreType = true; 092 } 093 if (hasScoreType) { 094 int score = topic.getCompositeValue().getModel().getInt(SOSO_TYPE_URI) + 1; 095 topic.getCompositeValue().set(SOSO_TYPE_URI, score, clientState, new Directives()); 096 tx.success(); 097 } else { 098 throw new WebApplicationException(new RuntimeException("The TypeDefinition (model) of the given topic " 099 + "does not contain the \"org.deepamehta.reviews.soso\"-type")); 100 } 101 } catch (Exception e) { 102 throw new WebApplicationException(new RuntimeException("something went wrong", e)); 103 } finally { 104 tx.finish(); 105 } 106 return topic; 107 } 108 109 /** Increments the score of any given topic. */ 110 111 @GET 112 @Path("/upvote/{id}") 113 @Produces("application/json") 114 @Override 115 public Topic upvoteResourceById(@PathParam("id") long resourceId, 116 @HeaderParam("Cookie") ClientState clientState) { 117 118 DeepaMehtaTransaction tx = dms.beginTx(); 119 Topic topic = null; 120 try { 121 // fixme: check if SCORE_URI is part of given topics type definition 122 topic = dms.getTopic(resourceId, true); 123 TopicType typeDef = dms.getTopicType(topic.getTypeUri()); 124 Collection<AssociationDefinitionModel> typeModel = typeDef.getModel().getAssocDefs(); 125 boolean hasScoreType = false; 126 for (AssociationDefinitionModel associationDefinitionModel : typeModel) { 127 if (associationDefinitionModel.getChildTypeUri().equals(SCORE_TYPE_URI)) hasScoreType = true; 128 } 129 if (hasScoreType) { 130 int score = topic.getCompositeValue().getModel().getInt(SCORE_TYPE_URI) + 1; 131 topic.getCompositeValue().set(SCORE_TYPE_URI, score, clientState, new Directives()); 132 tx.success(); 133 } else { 134 throw new WebApplicationException(new RuntimeException("The TypeDefinition (model) of the given topic " 135 + "does not contain the \"org.deepamehta.reviews.score\"-type")); 136 } 137 } catch (Exception e) { 138 throw new WebApplicationException(new RuntimeException("something went wrong", e)); 139 } finally { 140 tx.finish(); 141 } 142 return topic; 143 } 144 145 /** Decrements the score of any given topic. */ 146 147 @GET 148 @Path("/downvote/{id}") 149 @Produces("application/json") 150 @Override 151 public Topic downvoteResourceById(@PathParam("id") long resourceId, 152 @HeaderParam("Cookie") ClientState clientState) { 153 154 DeepaMehtaTransaction tx = dms.beginTx(); 155 Topic topic = null; 156 try { 157 // fixme: check if SCORE_URI is part of given topics type definition 158 topic = dms.getTopic(resourceId, true); 159 TopicType typeDef = dms.getTopicType(topic.getTypeUri()); 160 Collection<AssociationDefinitionModel> typeModel = typeDef.getModel().getAssocDefs(); 161 boolean hasScoreType = false; 162 for (AssociationDefinitionModel associationDefinitionModel : typeModel) { 163 if (associationDefinitionModel.getChildTypeUri().equals(SCORE_TYPE_URI)) hasScoreType = true; 164 } 165 if (hasScoreType) { 166 int score = topic.getCompositeValue().getModel().getInt(SCORE_TYPE_URI) - 1; 167 topic.getCompositeValue().set(SCORE_TYPE_URI, score, clientState, new Directives()); 168 tx.success(); 169 } else { 170 throw new WebApplicationException(new RuntimeException("The TypeDefinition (model) of the given topic " 171 + "does not contain the \"org.deepamehta.reviews.score\"-type")); 172 } 173 } catch (Exception e) { 174 throw new WebApplicationException(new RuntimeException("something went wrong", e)); 175 } finally { 176 tx.finish(); 177 } 178 return topic; 179 } 180 181 }