1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.messagestream.script

File MessageStreamScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
41
8
1
208
111
16
0.39
5.12
8
2

Classes

Class Line # Actions
MessageStreamScriptService 49 41 0% 16 4
0.924528392.5%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.messagestream.script;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.messagestream.MessageStream;
30    import org.xwiki.model.EntityType;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.EntityReference;
33    import org.xwiki.script.service.ScriptService;
34    import org.xwiki.stability.Unstable;
35   
36    import com.xpn.xwiki.XWiki;
37    import com.xpn.xwiki.XWikiContext;
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.objects.BaseObject;
40   
41    /**
42    * Service exposing the {@link MessageStream} functionality, allowing to post messages from the current user.
43    *
44    * @version $Id: 26243ac83aea937c992b2778af68b05eb33ff3b7 $
45    */
46    @Component
47    @Named("messageStream")
48    @Singleton
 
49    public class MessageStreamScriptService implements ScriptService
50    {
51    /**
52    * The key under which the last encountered error is stored in the current execution context.
53    */
54    static final String ERROR_KEY = "scriptservice.messageStream.error";
55   
56    private static final EntityReference CONFIG_DOCUMENT_REFERENCE =
57    new EntityReference("MessageStreamConfig", EntityType.DOCUMENT,
58    new EntityReference("XWiki", EntityType.SPACE));
59   
60    /**
61    * Provides access to the current context.
62    */
63    @Inject
64    protected Execution execution;
65   
66    /** The wrapped stream that is exposed in this service. */
67    @Inject
68    private MessageStream stream;
69   
70    /**
71    * Post a message to the user's stream, visible to everyone.
72    *
73    * @param message the message to store
74    * @return {@code true} if the message was successfully posted, {@code false} otherwise
75    */
 
76  2 toggle public boolean postPublicMessage(String message)
77    {
78  2 try {
79  2 this.stream.postPublicMessage(message);
80  1 return true;
81    } catch (Exception e) {
82  1 setError(e);
83  1 return false;
84    }
85    }
86   
87    /**
88    * Post a message to the user's personal stream, displayed on his profile page and aggregated into their follower's
89    * streams.
90    *
91    * @param message the message to store
92    * @return {@code true} if the message was successfully posted, {@code false} otherwise
93    */
 
94  2 toggle public boolean postPersonalMessage(String message)
95    {
96  2 try {
97  2 this.stream.postPersonalMessage(message);
98  1 return true;
99    } catch (Exception e) {
100  1 setError(e);
101  1 return false;
102    }
103    }
104   
105    /**
106    * Post a private message to another user.
107    *
108    * @param message the message to send
109    * @param user the target user
110    * @return {@code true} if the message was successfully posted, {@code false} otherwise
111    */
 
112  2 toggle public boolean postDirectMessageToUser(String message, DocumentReference user)
113    {
114  2 try {
115  2 this.stream.postDirectMessageToUser(message, user);
116  1 return true;
117    } catch (Exception e) {
118  1 setError(e);
119  1 return false;
120    }
121    }
122   
123    /**
124    * Post a message to a specific group of users.
125    *
126    * @param message the message to send
127    * @param group the target group
128    * @return {@code true} if the message was successfully posted, {@code false} otherwise
129    */
 
130  2 toggle public boolean postMessageToGroup(String message, DocumentReference group)
131    {
132  2 try {
133  2 this.stream.postMessageToGroup(message, group);
134  1 return true;
135    } catch (Exception e) {
136  1 setError(e);
137  1 return false;
138    }
139    }
140   
141    /**
142    * Delete an existing message, identified by its unique ID, if the current user is the author of that message.
143    *
144    * @param id the unique ID of the message
145    * @return {@code true} if the message was successfully deleted, {@code false} otherwise
146    */
 
147  2 toggle public boolean deleteMessage(String id)
148    {
149  2 try {
150  2 this.stream.deleteMessage(id);
151  1 return true;
152    } catch (Exception e) {
153  1 setError(e);
154  1 return false;
155    }
156    }
157   
158    /**
159    * @return true if the Message Stream feature is active or false otherwise
160    * @since 8.4RC1
161    */
 
162  80 toggle @Unstable
163    public boolean isActive()
164    {
165  80 boolean result = false;
166   
167    // TODO: Introduce a MessageStreamConfiguration class
168  80 try {
169  80 ExecutionContext ec = this.execution.getContext();
170  80 if (ec != null) {
171  80 XWikiContext xcontext = (XWikiContext) ec.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
172  80 if (xcontext != null) {
173  80 XWiki xwiki = xcontext.getWiki();
174  80 XWikiDocument configDocument = xwiki.getDocument(CONFIG_DOCUMENT_REFERENCE, xcontext);
175  80 BaseObject configObject = configDocument.getXObject(CONFIG_DOCUMENT_REFERENCE);
176  80 int active = configObject.getIntValue("active");
177  80 result = (active == 1);
178    }
179    }
180    } catch (Exception e) {
181  0 setError(e);
182  0 return false;
183    }
184   
185  80 return result;
186    }
187   
188    /**
189    * Get the error generated while performing the previously called action.
190    *
191    * @return the exception or {@code null} if no exception was thrown
192    */
 
193  4 toggle public Exception getLastError()
194    {
195  4 return (Exception) this.execution.getContext().getProperty(ERROR_KEY);
196    }
197   
198    /**
199    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
200    *
201    * @param e the exception to store, can be {@code null} to clear the previously stored exception
202    * @see #getLastError()
203    */
 
204  5 toggle protected void setError(Exception e)
205    {
206  5 this.execution.getContext().setProperty(ERROR_KEY, e);
207    }
208    }