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

File DefaultEventFactory.java

 

Coverage histogram

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

Code metrics

2
12
3
1
93
49
4
0.33
4
3
1.33

Classes

Class Line # Actions
DefaultEventFactory 44 12 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.eventstream.internal;
21   
22    import java.util.Date;
23    import java.util.UUID;
24   
25    import javax.inject.Inject;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.bridge.DocumentAccessBridge;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.context.Execution;
31    import org.xwiki.eventstream.Event;
32    import org.xwiki.eventstream.EventFactory;
33    import org.xwiki.model.EntityType;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.EntityReferenceResolver;
36   
37    /**
38    * Default implementation of the {@link EventFactory}, creating {@link DefaultEvent} objects as {@link Event events}.
39    *
40    * @version $Id: b1241c0ccb4249c0ca649b06084d9e81efa981c0 $
41    */
42    @Component
43    @Singleton
 
44    public class DefaultEventFactory implements EventFactory
45    {
46    /** The key used to store the current event group ID in the execution context. */
47    private static final String EVENT_GROUP_ID = Event.class.getCanonicalName().concat("_groupId");
48   
49    /** Needed for storing the current event group ID. */
50    @Inject
51    private Execution execution;
52   
53    /** Needed for converting the current username into a proper reference. */
54    @Inject
55    private EntityReferenceResolver<String> resolver;
56   
57    /** Needed for retrieving the current user. */
58    @Inject
59    private DocumentAccessBridge bridge;
60   
 
61  18 toggle @Override
62    public Event createEvent()
63    {
64  18 Event result = new DefaultEvent();
65  18 result.setId(UUID.randomUUID().toString());
66  18 result.setGroupId(getCurrentGroupId());
67  18 result.setUser(new DocumentReference(this.resolver.resolve(this.bridge.getCurrentUser(), EntityType.DOCUMENT)));
68  18 result.setDate(new Date());
69  18 return result;
70    }
71   
 
72  17 toggle @Override
73    public Event createRawEvent()
74    {
75  17 return new DefaultEvent();
76    }
77   
78    /**
79    * Retrieves the event group ID from the execution context. If there's no group ID in the context, generate a random
80    * one and store it in the context.
81    *
82    * @return the current event group ID
83    */
 
84  18 toggle private String getCurrentGroupId()
85    {
86  18 String currentGroup = (String) this.execution.getContext().getProperty(EVENT_GROUP_ID);
87  18 if (currentGroup == null) {
88  17 currentGroup = UUID.randomUUID().toString();
89  17 this.execution.getContext().setProperty(EVENT_GROUP_ID, currentGroup);
90    }
91  18 return currentGroup;
92    }
93    }