View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.quiz.service.outputprocessor;
35  
36  import fr.paris.lutece.portal.service.datastore.DatastoreService;
37  import fr.paris.lutece.portal.service.spring.SpringContextService;
38  
39  import java.util.List;
40  import java.util.Map;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  
45  /**
46   * Service to manage quiz output processors.
47   */
48  public final class QuizOutputProcessorManagementService
49  {
50      private static final String DATASTORE_KEY_QUIZ_OUTPUT_PROCESSOR_ENABLED = "quiz.outputProcessor.enabled.";
51      private static final String DATASTORE_KEY_QUIZ_OUTPUT_PROCESSOR_ENABLED_QUIZ = ".quiz.";
52      private static QuizOutputProcessorManagementService _instance = new QuizOutputProcessorManagementService( );
53  
54      /**
55       * Private constructor
56       */
57      private QuizOutputProcessorManagementService( )
58      {
59          // Do nothing
60      }
61  
62      /**
63       * Get the instance of the service
64       * @return The instance of the service
65       */
66      public static QuizOutputProcessorManagementService getInstance( )
67      {
68          return _instance;
69      }
70  
71      /**
72       * Get the list of every processors
73       * @return the list of every processors
74       */
75      public List<IQuizOutputProcessor> getProcessorsList( )
76      {
77          return SpringContextService.getBeansOfType( IQuizOutputProcessor.class );
78      }
79  
80      /**
81       * Get a processor from its id
82       * @param strProcessorId The id of the processor to get
83       * @return The processor, or null if no processor has the given id
84       */
85      public IQuizOutputProcessor getProcessor( String strProcessorId )
86      {
87          for ( IQuizOutputProcessor processor : getProcessorsList( ) )
88          {
89              if ( StringUtils.equals( strProcessorId, processor.getProcessorId( ) ) )
90              {
91                  return processor;
92              }
93          }
94          return null;
95      }
96  
97      /**
98       * Check if a processor is enabled or not for a given quiz
99       * @param strProcessorId The id of the processor to check
100      * @param nIdQuiz The id of the quiz
101      * @return True if the processor is enabled, false otherwise
102      */
103     public boolean isProcessorEnabled( String strProcessorId, int nIdQuiz )
104     {
105         String strDatastoreKey = getProcessorEnablingDatastoreKey( strProcessorId, nIdQuiz );
106         String strEnabled = DatastoreService.getDataValue( strDatastoreKey, null );
107         if ( strEnabled == null )
108         {
109             DatastoreService.setDataValue( strDatastoreKey, Boolean.FALSE.toString( ) );
110             return false;
111         }
112         return Boolean.parseBoolean( strEnabled );
113     }
114 
115     /**
116      * Enable or disable a processor for a given quiz. If the processor was
117      * enabled, it is disabled and vice versa
118      * @param nIdQuiz The id of the quiz
119      * @param strProcessorId The id of the processor to enable or disable
120      */
121     public void changeProcessorEnabling( String strProcessorId, int nIdQuiz )
122     {
123         boolean isEnabled = isProcessorEnabled( strProcessorId, nIdQuiz );
124         IQuizOutputProcessor processor = getProcessor( strProcessorId );
125         DatastoreService.setDataValue( getProcessorEnablingDatastoreKey( strProcessorId, nIdQuiz ),
126                 Boolean.toString( !isEnabled ) );
127         if ( isEnabled )
128         {
129             processor.notifyProcessorDisabling( nIdQuiz );
130         }
131         else
132         {
133             processor.notifyProcessorEnabling( nIdQuiz );
134         }
135     }
136 
137     /**
138      * Disable every enabled processors for a given quiz
139      * @param nIdQuiz the id of the quiz to disable processors of
140      */
141     public void disableProcessors( int nIdQuiz )
142     {
143         for ( IQuizOutputProcessor processor : getProcessorsList( ) )
144         {
145             if ( isProcessorEnabled( processor.getProcessorId( ), nIdQuiz ) )
146             {
147                 changeProcessorEnabling( processor.getProcessorId( ), nIdQuiz );
148             }
149         }
150     }
151 
152     /**
153      * Process every enabled processors for a given quiz over answers made by a
154      * user
155      * @param mapAnswers the map containing answers
156      * @param nIdQuiz The id of the answered quiz
157      */
158     public void processEnabledProcessors( Map<String, String[]> mapAnswers, String strScore, int nIdQuiz )
159     {
160         for ( IQuizOutputProcessor processor : getProcessorsList( ) )
161         {
162             if ( isProcessorEnabled( processor.getProcessorId( ), nIdQuiz ) )
163             {
164                 processor.doProcessOutputProcessor( mapAnswers, strScore, nIdQuiz );
165             }
166         }
167     }
168 
169     /**
170      * Get the datastore key to save the enabling of a processor for a quiz
171      * @param strProcessorId The id of the processor
172      * @param nIdQuiz The id of the quiz
173      * @return The datastore key
174      */
175     private String getProcessorEnablingDatastoreKey( String strProcessorId, int nIdQuiz )
176     {
177         return DATASTORE_KEY_QUIZ_OUTPUT_PROCESSOR_ENABLED + strProcessorId
178                 + DATASTORE_KEY_QUIZ_OUTPUT_PROCESSOR_ENABLED_QUIZ + nIdQuiz;
179     }
180 }