1 | |
package fr.paris.lutece.plugins.releaser.service; |
2 | |
|
3 | |
import java.util.Collection; |
4 | |
import java.util.Locale; |
5 | |
|
6 | |
import javax.servlet.http.HttpServletRequest; |
7 | |
|
8 | |
import org.apache.commons.collections.CollectionUtils; |
9 | |
|
10 | |
import fr.paris.lutece.plugins.releaser.business.Component; |
11 | |
import fr.paris.lutece.plugins.releaser.business.Site; |
12 | |
import fr.paris.lutece.plugins.releaser.business.WorkflowReleaseContext; |
13 | |
import fr.paris.lutece.plugins.releaser.util.ConstanteUtils; |
14 | |
import fr.paris.lutece.plugins.releaser.util.ReleaserUtils; |
15 | |
import fr.paris.lutece.plugins.workflowcore.business.action.Action; |
16 | |
import fr.paris.lutece.plugins.workflowcore.business.state.State; |
17 | |
import fr.paris.lutece.portal.business.user.AdminUser; |
18 | |
import fr.paris.lutece.portal.service.util.AppException; |
19 | |
import fr.paris.lutece.portal.service.util.AppLogService; |
20 | |
import fr.paris.lutece.portal.service.workflow.WorkflowService; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class ReleaseComponentTask implements Runnable { |
29 | |
|
30 | |
private static final long WAIT_TIME=1000; |
31 | |
private int _nIdWorkflow; |
32 | |
private WorkflowReleaseContext _wfContext; |
33 | |
private HttpServletRequest _request; |
34 | |
private AdminUser _user; |
35 | |
private Locale _locale; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public ReleaseComponentTask(int nIdWf,WorkflowReleaseContext context,HttpServletRequest request,AdminUser user,Locale locale) { |
41 | |
|
42 | 0 | _nIdWorkflow=nIdWf; |
43 | 0 | _wfContext=context; |
44 | 0 | _request=request; |
45 | 0 | _user=user; |
46 | 0 | _locale=locale; |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public void run( ){ |
56 | |
|
57 | 0 | State state = WorkflowService.getInstance( ) |
58 | 0 | .getState( _wfContext.getId( ), |
59 | |
WorkflowReleaseContext.WORKFLOW_RESOURCE_TYPE, _nIdWorkflow, |
60 | 0 | ConstanteUtils.CONSTANTE_ID_NULL ); |
61 | 0 | ReleaserUtils.startCommandResult( _wfContext ); |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
try |
70 | |
{ |
71 | |
|
72 | |
|
73 | 0 | if(_wfContext.getSite( ) !=null) |
74 | |
{ |
75 | 0 | while(!testAllComponentReleased( _wfContext.getSite( ) )) |
76 | |
{ |
77 | |
|
78 | |
try |
79 | |
{ |
80 | 0 | Thread.sleep( WAIT_TIME ); |
81 | |
} |
82 | 0 | catch( InterruptedException e ) |
83 | |
{ |
84 | 0 | AppLogService.error( e ); |
85 | 0 | } |
86 | |
} |
87 | |
|
88 | 0 | if(hasErrorDuringReleaseComponent( _wfContext.getSite( ) )) |
89 | |
{ |
90 | 0 | ReleaserUtils.addTechnicalError( _wfContext.getCommandResult( ), "The site can not be retrieved because one of component of the site is in error" ); |
91 | |
} |
92 | |
} |
93 | |
|
94 | |
|
95 | 0 | Collection<Action> listActions= WorkflowService.getInstance( ).getActions( _wfContext.getId( ), WorkflowReleaseContext.WORKFLOW_RESOURCE_TYPE,_nIdWorkflow,_user); |
96 | |
|
97 | 0 | for(Action action:listActions) |
98 | |
{ |
99 | 0 | WorkflowService.getInstance( ).doProcessAction( _wfContext.getId( ), WorkflowReleaseContext.WORKFLOW_RESOURCE_TYPE, action.getId( ), -1, _request, _locale, true ); |
100 | 0 | if(_wfContext.getComponent( )!=null) |
101 | |
{ |
102 | |
|
103 | 0 | ComponentService.getService( ).setLastReleaseVersion(_wfContext.getComponent( ).getArtifactId( ) ,_wfContext.getComponent( ).getTargetVersion( )); |
104 | 0 | _wfContext.getComponent( ).setLastAvailableVersion( _wfContext.getComponent( ).getTargetVersion( ) ); |
105 | 0 | ComponentService.getService( ).setLastReleaseNextSnapshotVersion(_wfContext.getComponent( ).getArtifactId( ) ,_wfContext.getComponent( ).getNextSnapshotVersion( )); |
106 | 0 | _wfContext.getComponent( ).setLastAvailableSnapshotVersion( _wfContext.getComponent( ).getNextSnapshotVersion( ) ); |
107 | |
} |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
} |
112 | 0 | catch( AppException appe ) |
113 | |
{ |
114 | 0 | if( _wfContext.getComponent( )!=null) |
115 | |
{ |
116 | |
|
117 | 0 | _wfContext.getComponent( ).setErrorLastRelease( true ); |
118 | |
} |
119 | 0 | AppLogService.error( appe ); |
120 | |
} |
121 | |
finally |
122 | |
{ |
123 | 0 | ReleaserUtils.stopCommandResult( _wfContext ); |
124 | 0 | WorkflowReleaseContextService.getService( ).saveWorkflowReleaseContext( _wfContext ); |
125 | 0 | } |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
private boolean testAllComponentReleased(Site site) |
131 | |
{ |
132 | 0 | if(!CollectionUtils.isEmpty(site.getComponents( ))) |
133 | |
{ |
134 | |
|
135 | 0 | for(Component component:site.getComponents( )) |
136 | |
{ |
137 | |
|
138 | 0 | if(!component.isErrorLastRelease( ) && component.shouldBeReleased( ) ) |
139 | |
{ |
140 | 0 | return false; |
141 | |
|
142 | |
} |
143 | 0 | } |
144 | |
} |
145 | |
|
146 | 0 | return true; |
147 | |
|
148 | |
} |
149 | |
|
150 | |
private boolean hasErrorDuringReleaseComponent(Site site) |
151 | |
{ |
152 | 0 | if(!CollectionUtils.isEmpty(site.getComponents( ))) |
153 | |
{ |
154 | |
|
155 | 0 | for(Component component:site.getComponents( )) |
156 | |
{ |
157 | |
|
158 | 0 | if(component.isErrorLastRelease( ) ) |
159 | |
{ |
160 | 0 | return true; |
161 | |
|
162 | |
} |
163 | 0 | } |
164 | |
} |
165 | |
|
166 | 0 | return false; |
167 | |
|
168 | |
} |
169 | |
|
170 | |
} |