-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDemandService.java
More file actions
230 lines (209 loc) · 9.24 KB
/
DemandService.java
File metadata and controls
230 lines (209 loc) · 9.24 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (c) 2002-2019, Mairie de Paris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* License 1.0
*/
package fr.paris.lutece.plugins.appcenter.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.paris.lutece.plugins.appcenter.business.Application;
import fr.paris.lutece.plugins.appcenter.business.ApplicationData;
import fr.paris.lutece.plugins.appcenter.business.ApplicationDatas;
import fr.paris.lutece.plugins.appcenter.business.ApplicationHome;
import fr.paris.lutece.plugins.appcenter.business.Demand;
import fr.paris.lutece.plugins.appcenter.business.DemandFilter;
import fr.paris.lutece.plugins.appcenter.business.DemandHome;
import fr.paris.lutece.plugins.appcenter.business.User;
import fr.paris.lutece.plugins.appcenter.business.UserHome;
import fr.paris.lutece.plugins.appcenter.web.DemandJspBean;
import fr.paris.lutece.portal.service.util.AppLogService;
import fr.paris.lutece.portal.service.workflow.WorkflowService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
/**
* Demand Service
*/
public class DemandService
{
private static final String PARAMETER_ENVIRONMENT_PREFIX = "environment_prefix";
private static final String PARAMETER_ID_DEMAND_TYPE = "id_demand_type";
private static final String PARAMETER_STATE = "state";
private static final String PARAMETER_APPLICATION = "application";
private static ObjectMapper _mapper = new ObjectMapper( );
public static void saveDemand( Demand demand, Application application )
{
demand.setCreationDate( new java.sql.Timestamp( ( new java.util.Date( ) ).getTime( ) ) );
demand.setDemandData( getDemandAsString( demand ) );
DemandHome.create( demand );
// Run the workflow
int nIdResource = demand.getId( );
int nIdWorkflow = DemandTypeService.getIdWorkflow( demand.getDemandType( ) );
WorkflowService.getInstance( ).getState( nIdResource, Demand.WORKFLOW_RESOURCE_TYPE, nIdWorkflow, -1 );
User user = UserHome.findByPrimaryKey( demand.getIdUserFront( ) );
UserService.addRBACRole( user, application.getId( ) );
WorkflowService.getInstance( ).doProcessAction( nIdResource, Demand.WORKFLOW_RESOURCE_TYPE, nIdWorkflow, -1 , null, Locale.FRENCH, false, user );
}
public static <T extends Demand> List<T> getDemandsListByApplicationAndType( Application application, String strDemandType, Class<T> demandClass )
{
return DemandHome.getDemandsListByApplicationAndType( application.getId( ), strDemandType, demandClass );
}
public static String getDemandAsString( Demand demand )
{
try
{
return _mapper.writeValueAsString( demand );
}
catch( JsonProcessingException e )
{
AppLogService.error( "Unable to convert demand obj to JSON", e );
}
return null;
}
/**
* Get the demand filter from the Http request
*
* @param request
* @return the demand filter
*/
public static DemandFilter computeDemandFilter( HttpServletRequest request )
{
String strEnvironmentPrefix = request.getParameter( PARAMETER_ENVIRONMENT_PREFIX );
String strIdDemandType = request.getParameter( PARAMETER_ID_DEMAND_TYPE );
String strState = request.getParameter( PARAMETER_STATE );
String strApplication = request.getParameter( PARAMETER_APPLICATION );
DemandFilter filter = new DemandFilter( );
if ( strEnvironmentPrefix != null && !strEnvironmentPrefix.equals( "-1" ) )
{
filter.setEnvironmentPrefix( strEnvironmentPrefix );
filter.setHasEnvironmentPrefix( true );
}
if ( strIdDemandType != null && !strIdDemandType.equals( "-1" ) )
{
filter.setIdDemandType( strIdDemandType );
filter.setHasIdDemandType( true );
}
if ( strState != null && !strState.equals( "-1" ) )
{
filter.setState( strState );
filter.setHasState( true );
}
if ( strApplication != null && !strApplication.equals( "" ) )
{
filter.setApplication( strApplication );
filter.setHasApplication( true );
}
return filter;
}
/**
* Remove the application whose identifier is specified in parameter with its dependencies
*
* @param nId
* The application Id
*/
public static void remove( int nId )
{
Demand demand = DemandHome.findByPrimaryKey( nId );
Application application = ApplicationHome.findByPrimaryKey( demand.getIdApplication( ) );
Class applicationDatasClass = DemandJspBean.getClassApplicationDatasByDemandTypeKey( demand.getDemandType( ) );
ApplicationDatas datas = null;
if ( applicationDatasClass != null )
{
datas = ApplicationService.loadApplicationDataSubset( application, applicationDatasClass );
}
if ( datas != null && demand.getIdApplicationData( ) != null )
{
// Modify data
ListIterator<ApplicationData> itr = datas.getListData( ).listIterator( );
while ( itr.hasNext( ) )
{
ApplicationData it = itr.next( );
if ( it.getIdApplicationData( ) == demand.getIdApplicationData( ) )
{
List<Integer> listIdDemandAssociated = it.getListIdDemandAssociated( );
if ( listIdDemandAssociated.contains( demand.getId( ) ) )
{
// remove the id in the list of demand associated
listIdDemandAssociated.remove( Integer.valueOf( demand.getId( ) ) );
}
if ( listIdDemandAssociated.size( ) == 0 )
{
// remove the applicationData
itr.remove( );
}
else
{
// update the list of demand associated
it.setListIdDemandAssociated( listIdDemandAssociated );
}
break;
}
}
ApplicationService.saveApplicationData( application, datas );
}
List<Integer> idResourceList = new ArrayList<Integer>( );
idResourceList.add( nId );
int nIdWorkflow = DemandTypeService.getIdWorkflow( demand.getDemandType( ) );
WorkflowService.getInstance( ).doRemoveWorkFlowResourceByListId( idResourceList, Demand.WORKFLOW_RESOURCE_TYPE, nIdWorkflow );
DemandHome.remove( nId );
}
/**
* Get the pretty print JSON data of a demand
*
* @param demand
* The demand
* @return The pretty printed JSON
* @throws IOException
* if an error occurs
*/
public static String getPrettyPrintDemandData( Demand demand )
{
String strDemandJson = demand.getDemandData( );
try
{
Object dataDemand = _mapper.readTree( strDemandJson );
if ( dataDemand != null )
{
strDemandJson = _mapper.writerWithDefaultPrettyPrinter( ).writeValueAsString( dataDemand );
}
}
catch( IOException ex )
{
Logger.getLogger( DemandService.class.getName( ) ).log( Level.WARNING, null, ex );
}
return strDemandJson;
}
}