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/dm4-reviews
025     * @version 0.3.6
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         * @param resourceId
043         * @param clientState */
044    
045        @GET
046        @Path("/good/{id}")
047        @Produces("application/json")
048        @Override
049        public Topic addToGood(@PathParam("id") long resourceId, 
050                @HeaderParam("Cookie") ClientState clientState) {
051    
052            DeepaMehtaTransaction tx = dms.beginTx();
053            Topic topic = null;
054            try {
055                // fixme: check if SCORE_URI is part of given topics type definition
056                topic = dms.getTopic(resourceId, true);
057                TopicType typeDef = dms.getTopicType(topic.getTypeUri());
058                Collection<AssociationDefinitionModel> typeModel = typeDef.getModel().getAssocDefs();
059                boolean hasScoreType = false;
060                for (AssociationDefinitionModel associationDefinitionModel : typeModel) {
061                    if (associationDefinitionModel.getChildTypeUri().equals(GOOD_TYPE_URI)) hasScoreType = true;
062                }
063                if (hasScoreType) {
064                    int score = topic.getCompositeValue().getModel().getInt(GOOD_TYPE_URI) + 1;
065                    topic.getCompositeValue().set(GOOD_TYPE_URI, score, clientState, new Directives());
066                    tx.success();
067                } else {
068                    throw new WebApplicationException(new RuntimeException("The TypeDefinition (model) of the given topic "
069                            + "does not contain the \"org.deepamehta.reviews.good\"-type"));
070                }
071            } catch (Exception e) {
072                throw new RuntimeException("something went wrong", e);
073            } finally {
074                tx.finish();
075            }
076            return topic;
077        }
078        
079        
080        /** Increments the number of supportive voices (yelling "Well, so so.").
081         * @param resourceId
082         * @param clientState */
083    
084        @GET
085        @Path("/soso/{id}")
086        @Produces("application/json")
087        @Override
088        public Topic addToSoso(@PathParam("id") long resourceId, 
089                @HeaderParam("Cookie") ClientState clientState) {
090    
091            DeepaMehtaTransaction tx = dms.beginTx();
092            Topic topic = null;
093            try {
094                // fixme: check if SCORE_URI is part of given topics type definition
095                topic = dms.getTopic(resourceId, true);
096                TopicType typeDef = dms.getTopicType(topic.getTypeUri());
097                Collection<AssociationDefinitionModel> typeModel = typeDef.getModel().getAssocDefs();
098                boolean hasScoreType = false;
099                for (AssociationDefinitionModel associationDefinitionModel : typeModel) {
100                    if (associationDefinitionModel.getChildTypeUri().equals(SOSO_TYPE_URI)) hasScoreType = true;
101                }
102                if (hasScoreType) {
103                    int score = topic.getCompositeValue().getModel().getInt(SOSO_TYPE_URI) + 1;
104                    topic.getCompositeValue().set(SOSO_TYPE_URI, score, clientState, new Directives());
105                    tx.success();
106                } else {
107                    throw new WebApplicationException(new RuntimeException("The TypeDefinition (model) of the given topic "
108                            + "does not contain the \"org.deepamehta.reviews.soso\"-type"));
109                }
110            } catch (Exception e) {
111                throw new RuntimeException("something went wrong", e);
112            } finally {
113                tx.finish();
114            }
115            return topic;
116        }
117    
118        /** Increments the score of any given topic.
119         * @param resourceId
120         * @param clientState */
121    
122        @GET
123        @Path("/upvote/{id}")
124        @Produces("application/json")
125        @Override
126        public Topic upvoteResourceById(@PathParam("id") long resourceId,
127                @HeaderParam("Cookie") ClientState clientState) {
128    
129            DeepaMehtaTransaction tx = dms.beginTx();
130            Topic topic = null;
131            try {
132                // fixme: check if SCORE_URI is part of given topics type definition
133                topic = dms.getTopic(resourceId, true);
134                TopicType typeDef = dms.getTopicType(topic.getTypeUri());
135                Collection<AssociationDefinitionModel> typeModel = typeDef.getModel().getAssocDefs();
136                boolean hasScoreType = false;
137                for (AssociationDefinitionModel associationDefinitionModel : typeModel) {
138                    if (associationDefinitionModel.getChildTypeUri().equals(SCORE_TYPE_URI)) hasScoreType = true;
139                }
140                if (hasScoreType) {
141                    int score = topic.getCompositeValue().getModel().getInt(SCORE_TYPE_URI) + 1;
142                    topic.getCompositeValue().set(SCORE_TYPE_URI, score, clientState, new Directives());
143                    tx.success();
144                } else {
145                    throw new RuntimeException("The TypeDefinition (model) of the given topic "
146                            + "does not contain the \"org.deepamehta.reviews.score\"-type");
147                }
148            } catch (Exception e) {
149                throw new RuntimeException("something went wrong", e);
150            } finally {
151                tx.finish();
152            }
153            return topic;
154        }
155    
156        /** Decrements the score of any given topic.
157         * @param resourceId
158         * @param clientState */
159    
160        @GET
161        @Path("/downvote/{id}")
162        @Produces("application/json")
163        @Override
164        public Topic downvoteResourceById(@PathParam("id") long resourceId,
165                @HeaderParam("Cookie") ClientState clientState) {
166    
167            DeepaMehtaTransaction tx = dms.beginTx();
168            Topic topic = null;
169            try {
170                // fixme: check if SCORE_URI is part of given topics type definition
171                topic = dms.getTopic(resourceId, true);
172                TopicType typeDef = dms.getTopicType(topic.getTypeUri());
173                Collection<AssociationDefinitionModel> typeModel = typeDef.getModel().getAssocDefs();
174                boolean hasScoreType = false;
175                for (AssociationDefinitionModel associationDefinitionModel : typeModel) {
176                    if (associationDefinitionModel.getChildTypeUri().equals(SCORE_TYPE_URI)) hasScoreType = true;
177                }
178                if (hasScoreType) {
179                    int score = topic.getCompositeValue().getModel().getInt(SCORE_TYPE_URI) - 1;
180                    topic.getCompositeValue().set(SCORE_TYPE_URI, score, clientState, new Directives());
181                    tx.success();
182                } else {
183                    throw new RuntimeException("The TypeDefinition (model) of the given topic "
184                            + "does not contain the \"org.deepamehta.reviews.score\"-type");
185                }
186            } catch (Exception e) {
187                throw new RuntimeException("something went wrong", e);
188            } finally {
189                tx.finish();
190            }
191            return topic;
192        }
193    
194    }