View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.greetingscard.service;
35  
36  import fr.paris.lutece.plugins.greetingscard.business.GreetingsCard;
37  import fr.paris.lutece.plugins.greetingscard.business.GreetingsCardArchive;
38  import fr.paris.lutece.plugins.greetingscard.business.GreetingsCardArchiveHome;
39  import fr.paris.lutece.plugins.greetingscard.business.GreetingsCardHome;
40  import fr.paris.lutece.plugins.greetingscard.business.GreetingsCardTemplate;
41  import fr.paris.lutece.plugins.greetingscard.business.GreetingsCardTemplateHome;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  
45  import java.util.ArrayList;
46  import java.util.Collection;
47  import java.util.Date;
48  
49  import org.apache.commons.lang3.StringUtils;
50  
51  
52  /**
53   * Greeting card service
54   */
55  public class GreetingsCardService
56  {
57  	public static final String beanName = "greetingsCardService";
58  
59  	private static final String PROPERTY_NB_LIMIT_ROWS = "greetingscard.archive.nbLimitRows";
60  	private static final String PROPERTY_ADRESS_EMAIL1 = "greetingscard.isInternal.Email1";
61  	private static final String PROPERTY_ADRESS_EMAIL2 = "greetingscard.isInternal.Email2";
62  
63  	private static final String CONSTANT_AT = "@";
64  	private static final String CONSTANT_COMA = ", ";
65  	private static final String CONSTANT_APOSTROPHE = "'";
66  
67  	/**
68  	 * Archive greetings card by template and date
69  	 * @param nIdGreetingsCardTemplate The greetings card template id. If 0, then every templates are archived
70  	 * @param nYear Year associated to created archives
71  	 * @param dateMin Minimum sent date of greetings cards to archive
72  	 * @param dateMax Maximum sent date of greetings cards to archive
73  	 * @param plugin The plugin
74  	 * @return The number of cards archived
75  	 */
76  	public int archiveGreetingsCards( int nIdGreetingsCardTemplate, int nYear, Date dateMin, Date dateMax, Plugin plugin )
77  	{
78  		int nResultsLimit = AppPropertiesService.getPropertyInt( PROPERTY_NB_LIMIT_ROWS, 0 );
79  		Collection<GreetingsCard> listGreetingsCard;
80  		Collection<GreetingsCardTemplate> listGreetingsCardTemplate;
81  		if ( nIdGreetingsCardTemplate > 0 )
82  		{
83  			listGreetingsCardTemplate = new ArrayList<GreetingsCardTemplate>( );
84  			GreetingsCardTemplate gct = new GreetingsCardTemplate( );
85  			gct.setId( nIdGreetingsCardTemplate );
86  			listGreetingsCardTemplate.add( gct );
87  		}
88  		else
89  		{
90  			listGreetingsCardTemplate = GreetingsCardTemplateHome.findAll( plugin );
91  		}
92  		int nIdGCT;
93  		int nNbArchivedCards = 0;
94  		for ( GreetingsCardTemplate gct : listGreetingsCardTemplate )
95  		{
96  			nIdGCT = gct.getId( );
97  			Collection<GreetingsCardArchive> listArchives = GreetingsCardArchiveHome.findByTemplateIdAndYear( nIdGCT, nYear, plugin );
98  
99  			while ( !( listGreetingsCard = GreetingsCardHome.findByTemplateAndDate( nIdGCT, dateMin, dateMax, nResultsLimit, plugin ) ).isEmpty( ) )
100 			{
101 				StringBuilder sbIdGC = null;
102 				for ( GreetingsCard greetingsCard : listGreetingsCard )
103 				{
104 					// We ignore copies
105 					if ( !greetingsCard.isCopy( ) )
106 					{
107 						String domainName = getDomainNameFromEmail( greetingsCard.getRecipientEmail( ) );
108 						GreetingsCardArchive archive = getArchiveFromDomainName( listArchives, domainName );
109 						if ( archive == null )
110 						{
111 							archive = new GreetingsCardArchive( );
112 							archive.setDomainName( domainName );
113 							archive.setYearArchive( nYear );
114 							archive.setIdGCT( nIdGCT );
115 							listArchives.add( archive );
116 						}
117 						archive.setNbCard( archive.getNbCard( ) + 1 );
118 						if ( greetingsCard.isRead( ) )
119 						{
120 							archive.setNbCardRed( archive.getNbCardRed( ) + 1 );
121 						}
122 					}
123 
124 					if ( sbIdGC == null )
125 					{
126 						sbIdGC = new StringBuilder( );
127 					}
128 					else
129 					{
130 						sbIdGC.append( CONSTANT_COMA );
131 					}
132 					sbIdGC.append( CONSTANT_APOSTROPHE );
133 					sbIdGC.append( greetingsCard.getId( ) );
134 					sbIdGC.append( CONSTANT_APOSTROPHE );
135 				}
136 				GreetingsCardHome.removeList( sbIdGC.toString( ), plugin );
137 				nNbArchivedCards += listGreetingsCard.size( );
138 			}
139 
140 			for ( GreetingsCardArchive archive : listArchives )
141 			{
142 				if ( archive.getIdArchive( ) > 0 )
143 				{
144 					GreetingsCardArchiveHome.update( archive, plugin );
145 				}
146 				else
147 				{
148 					GreetingsCardArchiveHome.insert( archive, plugin );
149 				}
150 			}
151 		}
152 
153 		return nNbArchivedCards;
154 	}
155 
156 	/**
157 	 * Test if the greeting card was sent to an internal person.
158 	 * @return the value of the test.
159 	 */
160 	public boolean isInternal( String strDomainName )
161 	{
162 		String strEndEmail1 = AppPropertiesService.getProperty( PROPERTY_ADRESS_EMAIL1 );
163 		String strEndEmail2 = AppPropertiesService.getProperty( PROPERTY_ADRESS_EMAIL2 );
164 
165 		if ( StringUtils.isNotBlank( strDomainName ) && ( strDomainName.equals( strEndEmail1 ) || strDomainName.equals( strEndEmail2 ) ) )
166 		{
167 			return true;
168 		}
169 
170 		return false;
171 	}
172 
173 	private String getDomainNameFromEmail( String strEmail )
174 	{
175 		if ( strEmail == null || !strEmail.contains( CONSTANT_AT ) )
176 		{
177 			return StringUtils.EMPTY;
178 		}
179 		return strEmail.substring( strEmail.indexOf( CONSTANT_AT ) );
180 	}
181 
182 	private GreetingsCardArchive getArchiveFromDomainName( Collection<GreetingsCardArchive> listArchives, String strDomainName )
183 	{
184 		for ( GreetingsCardArchive greetingsCardArchive : listArchives )
185 		{
186 			if ( StringUtils.equals( greetingsCardArchive.getDomainName( ), strDomainName ) )
187 			{
188 				return greetingsCardArchive;
189 			}
190 		}
191 		return null;
192 	}
193 }