1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
69
70
71
72
73
74
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
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
158
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 }