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.service;
35  
36  import fr.paris.lutece.plugins.digglike.business.CommentSubmit;
37  import fr.paris.lutece.plugins.digglike.business.CommentSubmitHome;
38  import fr.paris.lutece.plugins.digglike.business.SubmitFilter;
39  import fr.paris.lutece.plugins.digglike.utils.DiggUtils;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.spring.SpringContextService;
42  
43  import java.util.List;
44  
45  
46  public class CommentSubmitService implements ICommentSubmitService
47  {
48      public static final String BEAN_SERVICE = "digglike.commentSubmitService";
49      private static ICommentSubmitService _singleton;
50  
51      /**
52      * {@inheritDoc}
53      */
54      @Override
55      public void create( CommentSubmit commentSubmit, Plugin plugin )
56      {
57          commentSubmit.setDateModify( commentSubmit.getDateComment(  ) );
58          CommentSubmitHome.create( commentSubmit, plugin );
59  
60          if ( commentSubmit.getIdParent(  ) != DiggUtils.CONSTANT_ID_NULL )
61          {
62              //update parent modification date
63              CommentSubmitHome.updateDateModify( commentSubmit.getDateComment(  ), commentSubmit.getIdParent(  ), plugin );
64          }
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public void update( CommentSubmit commentSubmit, Plugin plugin )
72      {
73          CommentSubmitHome.update( commentSubmit, plugin );
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public void remove( int nIdCommentSubmit, Plugin plugin )
81      {
82          //    	SubmitFilter filter =new SubmitFilter();
83          //    	filter.setIdParent(nIdCommentSubmit);
84          //    	List<CommentSubmit> listSubComment= getCommentSubmitList(filter, plugin);
85          //    	for(CommentSubmit subComment:listSubComment)
86          //    	{
87          //    		CommentSubmitHome.remove( subComment.getIdCommentSubmit(), plugin );
88          //    	}
89          //remove children
90          CommentSubmitHome.removeByIdParent( nIdCommentSubmit, plugin );
91          CommentSubmitHome.remove( nIdCommentSubmit, plugin );
92      }
93  
94      ///////////////////////////////////////////////////////////////////////////
95      // Finders
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public CommentSubmit findByPrimaryKey( int nKey, Plugin plugin )
102     {
103         return CommentSubmitHome.findByPrimaryKey( nKey, plugin );
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public List<CommentSubmit> getCommentSubmitList( SubmitFilter filter, Plugin plugin )
111     {
112         if ( !filter.containsSortBy(  ) )
113         {
114             //use default sort
115             DiggUtils.initCommentFilterBySort( filter, DiggUtils.CONSTANT_ID_NULL );
116         }
117 
118         //get All parent
119         filter.setIdParent( SubmitFilter.ID_PARENT_NULL );
120 
121         List<CommentSubmit> commentSubmitList = CommentSubmitHome.getCommentSubmitList( filter, null, plugin );
122 
123         if ( commentSubmitList != null )
124         {
125             SubmitFilter subCommentFilter;
126 
127             for ( CommentSubmit c : commentSubmitList )
128             {
129                 subCommentFilter = new SubmitFilter(  );
130                 //in this  we gonna get themethod list of children of a comment
131                 subCommentFilter.setIdParent( c.getIdCommentSubmit(  ) );
132                 subCommentFilter.setIdCommentSubmitState( filter.getIdCommentSubmitState(  ) );
133                 subCommentFilter.getSortBy(  ).add( SubmitFilter.SORT_BY_DATE_RESPONSE_DESC );
134                 c.setComments( CommentSubmitHome.getCommentSubmitList( subCommentFilter, null, plugin ) );
135             }
136         }
137 
138         filter.setIdParent( DiggUtils.CONSTANT_ID_NULL );
139 
140         return commentSubmitList;
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public List<CommentSubmit> getCommentSubmitList( SubmitFilter filter, Integer nLimitParentNumber, Plugin plugin )
148     {
149         if ( ( nLimitParentNumber == null ) || ( nLimitParentNumber == DiggUtils.CONSTANT_ID_NULL ) )
150         {
151             //if the number of comment parent are not limited used getCommentSubmitList(filter, plugin)
152             return getCommentSubmitList( filter, plugin );
153         }
154 
155         if ( !filter.containsSortBy(  ) )
156         {
157             //use default sort
158             DiggUtils.initCommentFilterBySort( filter, DiggUtils.CONSTANT_ID_NULL );
159         }
160 
161         //get All parent
162         filter.setIdParent( SubmitFilter.ID_PARENT_NULL );
163 
164         List<CommentSubmit> commentSubmitList = CommentSubmitHome.getCommentSubmitList( filter, nLimitParentNumber,
165                 plugin );
166 
167         return commentSubmitList;
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public int getCountCommentSubmit( SubmitFilter filter, Plugin plugin )
175     {
176         return CommentSubmitHome.getCountCommentSubmit( filter, plugin );
177     }
178 
179     /**
180      * Returns the instance of the singleton
181      *
182      * @return The instance of the singleton
183      */
184     public static ICommentSubmitService getService(  )
185     {
186         if ( _singleton == null )
187         {
188             _singleton = SpringContextService.getBean( BEAN_SERVICE );
189         }
190 
191         return _singleton;
192     }
193 }