View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.genericattributes.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   *
44   * class EntryTypeDAO
45   *
46   */
47  public class EntryTypeDAO implements IEntryTypeDAO
48  {
49      private static final String SQL_QUERY_ORDER_BY = " order by display_order ASC ";
50      private static final String SQL_QUERY_SELECT_ALL = "SELECT id_type,title,is_group,is_comment,class_name,icon_name,is_mylutece_user,plugin,display_order,inactive"
51              + " FROM genatt_entry_type ";
52      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = SQL_QUERY_SELECT_ALL + " WHERE id_type=?";
53      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_ALL + " WHERE plugin = ? " + SQL_QUERY_ORDER_BY;
54      private static final String SQL_QUERY_UPDATE = "UPDATE genatt_entry_type SET title = ?,is_group = ?,is_comment = ?,class_name = ?,icon_name = ?,is_mylutece_user = ?,plugin = ?,display_order = ?,inactive = ? WHERE id_type = ? ";
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public EntryType load( int idKey, Plugin plugin )
61      {
62          EntryType entryType = null;
63          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
64          {
65              daoUtil.setInt( 1, idKey );
66              daoUtil.executeQuery( );
67  
68              if ( daoUtil.next( ) )
69              {
70                  entryType = dataToObject( daoUtil );
71              }
72          }
73          return entryType;
74      }
75  
76      @Override
77      public List<EntryType> selectAll( Plugin plugin )
78      {
79          List<EntryType> listEntryType = new ArrayList<>( );
80          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL + SQL_QUERY_ORDER_BY, plugin ) )
81          {
82              daoUtil.executeQuery( );
83  
84              while ( daoUtil.next( ) )
85              {
86                  EntryType entryType = dataToObject( daoUtil );
87                  listEntryType.add( entryType );
88              }
89          }
90          return listEntryType;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public List<EntryType> select( String strPlugin, Plugin plugin )
98      {
99          List<EntryType> listEntryType = new ArrayList<>( );
100         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
101         {
102             daoUtil.setString( 1, strPlugin );
103             daoUtil.executeQuery( );
104 
105             while ( daoUtil.next( ) )
106             {
107                 EntryType entryType = dataToObject( daoUtil );
108                 listEntryType.add( entryType );
109             }
110 
111         }
112 
113         return listEntryType;
114     }
115 
116     private EntryType dataToObject( DAOUtil daoUtil )
117     {
118         int index = 0;
119         EntryTypericattributes/business/EntryType.html#EntryType">EntryType entryType = new EntryType( );
120         entryType.setIdType( daoUtil.getInt( ++index ) );
121         entryType.setTitle( daoUtil.getString( ++index ) );
122         entryType.setGroup( daoUtil.getBoolean( ++index ) );
123         entryType.setComment( daoUtil.getBoolean( ++index ) );
124         entryType.setBeanName( daoUtil.getString( ++index ) );
125         entryType.setIconName( daoUtil.getString( ++index ) );
126         entryType.setMyLuteceUser( daoUtil.getBoolean( ++index ) );
127         entryType.setPlugin( daoUtil.getString( ++index ) );
128         entryType.setOrder( daoUtil.getInt( ++index ) );
129         entryType.setInactive( daoUtil.getBoolean( ++index ) );
130 
131         return entryType;
132     }
133 
134     @Override
135     public void store( EntryType entryType, Plugin plugin )
136     {
137         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
138         {
139             int index = 0;
140             daoUtil.setString( ++index, entryType.getTitle( ) );
141             daoUtil.setBoolean( ++index, entryType.getGroup( ) );
142             daoUtil.setBoolean( ++index, entryType.getComment( ) );
143             daoUtil.setString( ++index, entryType.getBeanName( ) );
144             daoUtil.setString( ++index, entryType.getIconName( ) );
145             daoUtil.setBoolean( ++index, entryType.getMyLuteceUser( ) );
146             daoUtil.setString( ++index, entryType.getPlugin( ) );
147             daoUtil.setInt( ++index, entryType.getOrder( ) );
148             daoUtil.setBoolean( ++index, entryType.isInactive( ) );
149 
150             daoUtil.setInt( ++index, entryType.getIdType( ) );
151 
152             daoUtil.executeUpdate( );
153         }
154 
155     }
156 }