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.digglike.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  public class ReportedMessageDAO implements IReportedMessageDAO
44  {
45      // Constants
46      private static final String SQL_FILTER_ID_DIGG_SUBMIT = " id_digg_submit = ? ";
47      private static final String SQL_FILTER_ID_REPORTED_MESSAGE = " id_reported_message = ? ";
48      private static final String SQL_ORDER_BY_DATE = " ORDER BY date_reported DESC ";
49      private static final String SQL_QUERY_NEW_PK = "SELECT MAX( id_reported_message ) FROM digglike_reported_message";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO digglike_reported_message ( id_reported_message,id_digg_submit,date_reported,reported_value)" +
51          " VALUES(?,?,?,?)";
52      private static final String SQL_QUERY_FIND = "SELECT id_reported_message,id_digg_submit,date_reported,reported_value " +
53          "FROM digglike_reported_message WHERE ";
54      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = SQL_QUERY_FIND + SQL_FILTER_ID_REPORTED_MESSAGE;
55      private static final String SQL_QUERY_FIND_BY_DIGG_SUBMIT = SQL_QUERY_FIND + SQL_FILTER_ID_DIGG_SUBMIT +
56          SQL_ORDER_BY_DATE;
57      private static final String SQL_QUERY_DELETE_BY_DIGG_SUBMIT = "DELETE FROM digglike_reported_message WHERE " +
58          SQL_FILTER_ID_DIGG_SUBMIT;
59  
60      /**
61      * {@inheritDoc}
62      */
63      @Override
64      public void insert( ReportedMessage reportedMessage, Plugin plugin )
65      {
66          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
67          reportedMessage.setIdReported( newPrimaryKey( plugin ) );
68          daoUtil.setInt( 1, reportedMessage.getIdReported(  ) );
69          daoUtil.setInt( 2, reportedMessage.getDiggSubmit(  ).getIdDiggSubmit(  ) );
70          daoUtil.setTimestamp( 3, reportedMessage.getDateReported(  ) );
71          daoUtil.setString( 4, reportedMessage.getValue(  ) );
72          daoUtil.executeUpdate(  );
73          daoUtil.free(  );
74      }
75  
76      /**
77      * {@inheritDoc}
78      */
79      @Override
80      public ReportedMessage load( int nKey, Plugin plugin )
81      {
82          // TODO Auto-generated method stub
83          ReportedMessage reportedMessage = null;
84  
85          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
86          daoUtil.setInt( 1, nKey );
87          daoUtil.executeQuery(  );
88  
89          if ( daoUtil.next(  ) )
90          {
91              reportedMessage = getRow( daoUtil );
92          }
93  
94          daoUtil.free(  );
95  
96          return reportedMessage;
97      }
98  
99      /**
100     * {@inheritDoc}
101     */
102     @Override
103     public void deleteByDiggSubmit( int nIdDiggSubmit, Plugin plugin )
104     {
105         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_DIGG_SUBMIT, plugin );
106         daoUtil.setInt( 1, nIdDiggSubmit );
107         daoUtil.executeUpdate(  );
108         daoUtil.free(  );
109     }
110 
111     /**
112     * {@inheritDoc}
113     */
114     @Override
115     public List<ReportedMessage> selectListByDiggSubmit( int nIdDiggSubmit, Plugin plugin )
116     {
117         List<ReportedMessage> reportedMessageList = new ArrayList<ReportedMessage>(  );
118 
119         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_DIGG_SUBMIT, plugin );
120         daoUtil.setInt( 1, nIdDiggSubmit );
121         daoUtil.executeQuery(  );
122 
123         while ( daoUtil.next(  ) )
124         {
125             reportedMessageList.add( getRow( daoUtil ) );
126         }
127 
128         daoUtil.free(  );
129 
130         return reportedMessageList;
131     }
132 
133     /**
134     * Generates a new primary key
135     *
136     * @param plugin the plugin
137     * @return The new primary key
138     */
139     private int newPrimaryKey( Plugin plugin )
140     {
141         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
142         daoUtil.executeQuery(  );
143 
144         int nKey;
145 
146         if ( !daoUtil.next(  ) )
147         {
148             // if the table is empty
149             nKey = 1;
150         }
151 
152         nKey = daoUtil.getInt( 1 ) + 1;
153         daoUtil.free(  );
154 
155         return nKey;
156     }
157 
158     /**
159      * return  ReportedMessage
160      * @param daoUtil ReportedMessage
161      * @return ReportedMessage
162      */
163     private ReportedMessage getRow( DAOUtil daoUtil )
164     {
165         ReportedMessage reportedMessage = null;
166         DiggSubmit diggSubmit = null;
167 
168         reportedMessage = new ReportedMessage(  );
169         reportedMessage.setIdReported( daoUtil.getInt( 1 ) );
170 
171         diggSubmit = new DiggSubmit(  );
172         diggSubmit.setIdDiggSubmit( daoUtil.getInt( 2 ) );
173         reportedMessage.setDiggSubmit( diggSubmit );
174 
175         reportedMessage.setDateReported( daoUtil.getTimestamp( 3 ) );
176         reportedMessage.setValue( daoUtil.getString( 4 ) );
177 
178         return reportedMessage;
179     }
180 }