View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.event.comment.list;
23  
24  import org.opencastproject.event.comment.EventCommentException;
25  import org.opencastproject.event.comment.EventCommentService;
26  import org.opencastproject.list.api.ListProviderException;
27  import org.opencastproject.list.api.ResourceListProvider;
28  import org.opencastproject.list.api.ResourceListQuery;
29  
30  import org.osgi.framework.BundleContext;
31  import org.osgi.service.component.annotations.Activate;
32  import org.osgi.service.component.annotations.Component;
33  import org.osgi.service.component.annotations.Reference;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  @Component(
43      service = ResourceListProvider.class,
44      property = {
45          "service.description=Comments list provider",
46          "opencast.service.type=org.opencastproject.list.provider.EventCommentsListProvider"
47      }
48  )
49  public class EventCommentsListProvider implements ResourceListProvider {
50  
51    private static final Logger logger = LoggerFactory.getLogger(EventCommentsListProvider.class);
52  
53    public static final String PROVIDER_PREFIX = "comments";
54  
55    /** The list of filter criteria for this provider */
56    public enum CommentsFilterList {
57      REASON, RESOLUTION;
58    };
59  
60    /** The resolutions */
61    public enum RESOLUTION {
62      ALL, UNRESOLVED, RESOLVED;
63    };
64  
65    /** The names of the different list available through this provider */
66    private final List<String> listNames = new ArrayList<String>();
67  
68    private EventCommentService eventCommentService;
69  
70    @Activate
71    protected void activate(BundleContext bundleContext) {
72      // Fill the list names
73      for (CommentsFilterList value : CommentsFilterList.values()) {
74        listNames.add(getListNameFromFilter(value));
75      }
76  
77      logger.info("Comments list provider activated!");
78    }
79  
80    /** OSGi callback for the event comment service. */
81    @Reference
82    public void setEventCommentService(EventCommentService eventCommentService) {
83      this.eventCommentService = eventCommentService;
84    }
85  
86    @Override
87    public String[] getListNames() {
88      return listNames.toArray(new String[listNames.size()]);
89    }
90  
91    @Override
92    public Map<String, String> getList(String listName, ResourceListQuery query)
93            throws ListProviderException {
94      Map<String, String> result = new HashMap<String, String>();
95  
96      if (CommentsFilterList.REASON.equals(listName)) {
97        List<String> reasons;
98        try {
99          reasons = eventCommentService.getReasons();
100       } catch (EventCommentException e) {
101         logger.error("Error retreiving reasons from event comment service", e);
102         throw new ListProviderException("Error retreiving reasons from event comment service", e);
103       }
104 
105       for (String reason : reasons) {
106         result.put(reason, reason);
107       }
108     } else if (CommentsFilterList.RESOLUTION.equals(listName)) {
109       for (RESOLUTION value : RESOLUTION.values()) {
110         result.put(value.toString(), value.toString());
111       }
112     } else {
113       logger.warn("No comments list for list name {} found", listName);
114       throw new ListProviderException("No comments list for list name " + listName + " found!");
115     }
116 
117     return result;
118   }
119 
120   /**
121    * Returns the list name related to the given filter
122    *
123    * @param filter
124    *          the filter from which the list name is needed
125    * @return the list name related to the given filter
126    */
127   public static String getListNameFromFilter(CommentsFilterList filter) {
128     return PROVIDER_PREFIX.toLowerCase() + "_" + filter.toString().toLowerCase();
129   }
130 
131   @Override
132   public boolean isTranslatable(String listName) {
133     return true;
134   }
135 
136   @Override
137   public String getDefault() {
138     return null;
139   }
140 }