1    /*
2     * Copyright 2009 :torweg free software group
3     *
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.component.statistics.view;
19   
20   import org.slf4j.Logger;
21   import org.slf4j.LoggerFactory;
22   import org.torweg.pulse.accesscontrol.Role;
23   import org.torweg.pulse.accesscontrol.User;
24   import org.torweg.pulse.bundle.Controller;
25   import org.torweg.pulse.component.statistics.model.StatisticsServer;
26   import org.torweg.pulse.configuration.Configurable;
27   import org.torweg.pulse.configuration.Configuration;
28   import org.torweg.pulse.service.request.ServiceRequest;
29   
30   /**
31    * Abstract base-class to derive a pulse website administration "view" for the
32    * statistics component from.
33    * <p>
34    * Basically this will be:
35    * </p>
36    * <ul>
37    * <li>the statistics west panel</li>
38    * <li>the "statistical data viewers"</li>
39    * </ul>
40    * 
41    * @param <T>
42    *            the actual implementation of:
43    *            {@code AbstractStatisticsViewControllerConfiguration}
44    * @param <U>
45    *            the actual implementation of:
46    *            {@code AbstractStatisticsViewControllerResult}
47    * 
48    * @author Daniel Dietz
49    * @version $Revision: 1914 $
50    */
51   public abstract class AbstractStatisticsController<T extends AbstractStatisticsViewControllerConfiguration, U extends AbstractStatisticsViewControllerResult<?>>
52           extends Controller implements
53           Configurable<AbstractStatisticsViewControllerConfiguration> {
54   
55       /**
56        * The LOGGER.
57        */
58       protected static final Logger LOGGER = LoggerFactory
59               .getLogger(AbstractStatisticsController.class);
60       /**
61        * The {@code AbstractStatisticsViewControllerConfiguration}.
62        */
63       private T config;
64   
65       /**
66        * Initialises the view.
67        * 
68        * @param request
69        *            the current {@code ServiceRequest}
70        * 
71        * @return a JAXB-enabled
72        *         {@code AbstractStatisticsViewControllerResult&lt;? extends AbstractStatisticsViewControllerResultData&gt;}
73        */
74       public abstract U initView(ServiceRequest request);
75   
76       /**
77        * Initialises the {@code Controller} with the given {@code Configuration}.
78        * 
79        * @param conf
80        *            the {@code Configuration}
81        * 
82        */
83       public final void initialize(
84               final AbstractStatisticsViewControllerConfiguration conf) {
85           setConfiguration(conf);
86       }
87   
88       /**
89        * Sets the {@code Configuration}.
90        * 
91        * @param conf
92        *            the {@code Configuration}
93        */
94       @SuppressWarnings("unchecked")
95       private void setConfiguration(final Configuration conf) {
96           this.config = (T) conf;
97       }
98   
99       /**
100       * Returns the {@code AbstractStatisticsViewControllerConfiguration}.
101       * 
102       * @return {@code AbstractStatisticsViewControllerConfiguration}
103       */
104      protected final T getConfiguration() {
105          return this.config;
106      }
107  
108      /**
109       * Checks whether the given {@code User} has the {@code Role} required for
110       * the given {@code StatisticsServer}.
111       * 
112       * @param user
113       *            the {@code User}
114       * @param server
115       *            the {@code StatisticsServer}
116       * 
117       * @return {@code true} if end only if the given {@code User} has the
118       *         matching {@code Role}, {@code false} otherwise
119       */
120      protected final boolean hasRoleForServer(final User user,
121              final StatisticsServer server) {
122          if (user.isSuperuser()) {
123              return true;
124          }
125          for (Role role : user.getRoles()) {
126              if (role.getName().equals(server.getStatisticsRoleName())) {
127                  return true;
128              }
129          }
130          return false;
131      }
132  
133  }
134