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.dbpage.service;
35  
36  import fr.paris.lutece.plugins.dbpage.business.DbPage;
37  import fr.paris.lutece.plugins.dbpage.business.DbPageDatabaseSection;
38  import fr.paris.lutece.portal.service.resource.ResourceService;
39  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
40  import fr.paris.lutece.util.ReferenceList;
41  
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.List;
45  
46  import javax.servlet.http.HttpServletRequest;
47  
48  
49  /**
50   * This class provides DbPageService object
51   */
52  public final class DbPageService extends ResourceService
53  {
54      private static DbPageService _singleton = new DbPageService(  );
55      private static final String PROPERTY_NAME = "dbpage.service.name";
56      private static final String PROPERTY_CACHE = "dbpage.service.cache";
57      private static final String PROPERTY_LOADERS = "dbpage.service.loaders";
58      private static final String PARAMETER_DBPAGE_VALUE = "value";
59  
60      /**
61       * Private constructor
62       */
63      private DbPageService(  )
64      {
65          super(  );
66          setCacheKey( PROPERTY_CACHE );
67          setNameKey( PROPERTY_NAME );
68      }
69  
70      /**
71       * Initialize the DbPage service
72       *
73       */
74      public void init(  )
75      {
76          DbPage.init(  );
77          DbPageDatabaseSection.init(  );
78      }
79  
80      /**
81       * Returns the instance of the singleton
82       *
83       * @return The instance of the singleton
84       */
85      public static DbPageService getInstance(  )
86      {
87          return _singleton;
88      }
89  
90      /**
91       * Returns the DbPage
92       *
93       * @param strDbPageName The DbPage's name
94       * @return the DbPage
95       */
96      public DbPage getDbPage( String strDbPageName )
97      {
98          DbPage dbPage = (DbPage) getResource( strDbPageName );
99  
100         if ( dbPage != null )
101         {
102             dbPage.setWorkgroup( AdminWorkgroupService.normalizeWorkgroupKey( dbPage.getWorkgroup(  ) ) );
103         }
104 
105         return dbPage;
106     }
107 
108     /**
109      * Returns a Collection of all dbpages
110      * @return The Collection of all dbpages
111      */
112     public Collection<DbPage> getDbPagesCollection(  )
113     {
114         Collection colDbPages = getResources(  );
115 
116         for ( DbPage dbPage : (Collection<DbPage>) colDbPages )
117         {
118             dbPage.setWorkgroup( AdminWorkgroupService.normalizeWorkgroupKey( dbPage.getWorkgroup(  ) ) );
119         }
120 
121         return colDbPages;
122     }
123 
124     /**
125      * Returns a list of all dbpages
126      * @return The list of all dbpages
127      */
128     public ReferenceList getDbPagesList(  )
129     {
130         ReferenceList list = new ReferenceList(  );
131 
132         for ( DbPage dbPage : getDbPagesCollection(  ) )
133         {
134             list.addItem( dbPage.getName(  ), dbPage.getTitle(  ) );
135         }
136 
137         return list;
138     }
139 
140     /**
141      * Extracts values from the Http request
142      * @param request The Http request
143      * @return A list of values
144      */
145     public List<String> getValues( HttpServletRequest request )
146     {
147         List<String> list = new ArrayList<String>(  );
148         int i = 0;
149         String strValue = request.getParameter( PARAMETER_DBPAGE_VALUE + ( i + 1 ) );
150 
151         while ( strValue != null )
152         {
153             list.add( i, strValue );
154             i++;
155             strValue = request.getParameter( PARAMETER_DBPAGE_VALUE + ( i + 1 ) );
156         }
157 
158         return list;
159     }
160 
161     /**
162      * Returns the property key that contains the loaders list
163      * @return A property key
164      */
165     protected String getLoadersProperty(  )
166     {
167         return PROPERTY_LOADERS;
168     }
169 }