1 /*
2 * Copyright (c) 2002-2021, 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.newsletter.business.topic;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.portal.service.spring.SpringContextService;
38
39 import java.util.List;
40
41 /**
42 * Home for NewsletterTopic objects
43 */
44 public final class NewsletterTopicHome
45 {
46 private static INewsletterTopicDAO _dao = SpringContextService.getBean( "newsletter.newsletterTopicDAO" );
47
48 /**
49 * Private constructor
50 */
51 private NewsletterTopicHome( )
52 {
53 }
54
55 /**
56 * Get a {@link NewsletterTopic} by its primary key from the database
57 *
58 * @param nId
59 * The id of the {@link NewsletterTopic} to get
60 * @param plugin
61 * The plugin
62 * @return The {@link NewsletterTopic} with the given id, or null if no {@link NewsletterTopic} has this id.
63 */
64 public static NewsletterTopic findByPrimaryKey( int nId, Plugin plugin )
65 {
66 return _dao.findByPrimaryKey( nId, plugin );
67 }
68
69 /**
70 * Insert a new {@link NewsletterTopic} into the database
71 *
72 * @param newsletterTopic
73 * The {@link NewsletterTopic} to insert.
74 * @param plugin
75 * The plugin
76 */
77 public static void insertNewsletterTopic( NewsletterTopic newsletterTopic, Plugin plugin )
78 {
79 newsletterTopic.setOrder( _dao.getNewOrder( newsletterTopic.getIdNewsletter( ), newsletterTopic.getSection( ), plugin ) );
80 _dao.insert( newsletterTopic, plugin );
81 }
82
83 /**
84 * Update a {@link NewsletterTopic} in the database
85 *
86 * @param newsletterTopic
87 * The new values of the {@link NewsletterTopic}.
88 * @param plugin
89 * The plugin
90 */
91 public static void updateNewsletterTopic( NewsletterTopic newsletterTopic, Plugin plugin )
92 {
93 _dao.update( newsletterTopic, plugin );
94 }
95
96 /**
97 * Delete a {@link NewsletterTopic} from the database
98 *
99 * @param nId
100 * The id of the {@link NewsletterTopic} to delete.
101 * @param plugin
102 * The plugin
103 */
104 public static void removeNewsletterTopic( int nId, Plugin plugin )
105 {
106 _dao.remove( nId, plugin );
107 }
108
109 /**
110 * Get the list of {@link NewsletterTopic} associated with a given newsletter.
111 *
112 * @param nIdNewsletter
113 * The id of the newsletter
114 * @param plugin
115 * The plugin
116 */
117 public static void removeAllByIdNewsletter( int nIdNewsletter, Plugin plugin )
118 {
119 _dao.removeAllByIdNewsletter( nIdNewsletter, plugin );
120 }
121
122 /**
123 * Get the list of {@link NewsletterTopic} associated with a given newsletter.
124 *
125 * @param nIdNewsletter
126 * The id of the newsletter
127 * @param plugin
128 * The plugin
129 * @return The list of {@link NewsletterTopic} found.
130 */
131 public static List<NewsletterTopic> findAllByIdNewsletter( int nIdNewsletter, Plugin plugin )
132 {
133 return _dao.findAllByIdNewsletter( nIdNewsletter, plugin );
134 }
135
136 /**
137 * Update topic orders of a newsletter. The order of the given topic is set to the new value, and the topic that had this order gets the old order of the
138 * updated topic.
139 *
140 * @param newsletterTopic
141 * The topic to move. The order attribute of the topic <b>MUST</b> be its old order.
142 * @param nNewOrder
143 * The new order of the topic
144 * @param plugin
145 * The plugin
146 */
147 public static void updateNewsletterTopicOrder( NewsletterTopic newsletterTopic, int nNewOrder, Plugin plugin )
148 {
149 List<NewsletterTopic> listTopics = _dao.findByNewsletterIdAndOrder( newsletterTopic.getIdNewsletter( ), nNewOrder, newsletterTopic.getSection( ),
150 plugin );
151 if ( listTopics != null && listTopics.size( ) > 0 )
152 {
153 _dao.updateNewsletterTopicOrder( listTopics.get( 0 ).getId( ), newsletterTopic.getOrder( ), plugin );
154 if ( listTopics.size( ) > 1 )
155 {
156 listTopics.remove( 0 );
157 int nNextOrder = _dao.getNewOrder( newsletterTopic.getIdNewsletter( ), newsletterTopic.getSection( ), plugin );
158 for ( NewsletterTopic topic : listTopics )
159 {
160 _dao.updateNewsletterTopicOrder( topic.getId( ), nNextOrder++, plugin );
161 }
162 }
163 }
164 _dao.updateNewsletterTopicOrder( newsletterTopic.getId( ), nNewOrder, plugin );
165 }
166
167 /**
168 * Get the next available order value for topics of a newsletter
169 *
170 * @param nIdNewsletter
171 * The id of the newsletter
172 * @param nSection
173 * The section
174 * @param plugin
175 * The plugin
176 * @return The next available order value
177 */
178 public static int getNewOrder( int nIdNewsletter, int nSection, Plugin plugin )
179 {
180 return _dao.getNewOrder( nIdNewsletter, nSection, plugin );
181 }
182
183 /**
184 * Get the highest order for a given newsletter and a given section
185 *
186 * @param nIdNewsletter
187 * The id of the newsletter
188 * @param nSection
189 * The id of the section
190 * @param plugin
191 * The plugin
192 * @return The highest order actually used for the given newsletter and the given section
193 */
194 public static int getLastOrder( int nIdNewsletter, int nSection, Plugin plugin )
195 {
196 return _dao.getLastOrder( nIdNewsletter, nSection, plugin );
197 }
198
199 /**
200 * Fill a blank in the order of topics of a newsletter.
201 *
202 * @param nIdNewsletter
203 * The newsletter to update the topics of
204 * @param nOrder
205 * The order with no topic
206 * @param nSection
207 * The section of topics to update
208 * @param plugin
209 * the plugin
210 */
211 public static void fillBlankInOrder( int nIdNewsletter, int nOrder, int nSection, Plugin plugin )
212 {
213 _dao.fillBlankInOrder( nIdNewsletter, nOrder, nSection, plugin );
214 }
215 }