View Javadoc
1   package fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web;
2   
3   import java.util.HashMap;
4   import java.util.Locale;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import javax.inject.Inject;
9   import javax.servlet.http.HttpServletRequest;
10  import javax.validation.ConstraintViolation;
11  
12  
13  import fr.paris.lutece.plugins.appcenter.business.Application;
14  import fr.paris.lutece.plugins.appcenter.business.ApplicationHome;
15  import fr.paris.lutece.plugins.appcenter.business.Demand;
16  import fr.paris.lutece.plugins.appcenter.business.DemandHome;
17  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
18  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationDemand;
19  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.InvalidRepositoryUrlException;
20  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.RepositoryUtils;
21  import fr.paris.lutece.plugins.appcenter.web.Constants;
22  import fr.paris.lutece.plugins.workflow.web.task.NoConfigTaskComponent;
23  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
24  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
25  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
26  import fr.paris.lutece.portal.service.datastore.DatastoreService;
27  import fr.paris.lutece.portal.service.template.AppTemplateService;
28  import fr.paris.lutece.util.ReferenceList;
29  import fr.paris.lutece.util.bean.BeanUtil;
30  import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
31  import fr.paris.lutece.util.html.HtmlTemplate;
32  
33  public class FastDeployApplicationTaskComponent extends NoConfigTaskComponent
34  {
35      // TEMPLATES
36      private static final String TEMPLATE_FASTDEPLOY_TASK_FORM = "admin/plugins/appcenter/modules/fastdeployapplication/fastdeploy_task_form.html";
37      private static final String TEMPLATE_FAST_DEPLOY_DISPLAY_HISTORY = "admin/plugins/appcenter/modules/fastdeployapplication/fastdeploy_task_history.html";
38  
39      // MESSAGES
40      private static final String MESSAGE_MANDATORY_FIELD = "portal.util.message.mandatoryFields";
41  
42      @Inject
43      private IResourceHistoryService _resourceHistoryService;
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public String getDisplayTaskForm( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
50      {
51          Map<String, Object> model = new HashMap<String, Object>( );
52  
53          FastDeployApplicationDemand demand = DemandHome.findByPrimaryKey( nIdResource, FastDeployApplicationDemand.class );
54          if ( demand != null )
55          {
56              model.put( Constants.MARK_DEMAND, demand );
57              Application application = ApplicationHome.findByPrimaryKey( demand.getIdApplication( ) );
58              model.put( Constants.MARK_APPLICATION, application );
59              ReferenceList refServices=DatastoreService.getDataByPrefix( FastDeployApplicationsXPage.DATA_PREFIX_FAST_DEPLOY_SERVICES );
60               model.put( FastDeployApplicationsXPage.MARK_SERVICES, refServices);
61               model.put( FastDeployApplicationsXPage.MARK_MAP_SERVICES, refServices.toMap( ) );
62          }
63  
64          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FASTDEPLOY_TASK_FORM, locale, model );
65  
66          return template.getHtml( );
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public String doValidateTask( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
74      {
75          FastDeployApplicationDataastdeployapplication/business/FastDeployApplicationData.html#FastDeployApplicationData">FastDeployApplicationData fastDeployData = new FastDeployApplicationData( );
76          BeanUtil.populate( fastDeployData, request );
77          
78          
79          
80          //Check if the url of the repository is valid
81          try
82          {
83              RepositoryUtils.checkRepository( fastDeployData );
84          }
85          catch ( InvalidRepositoryUrlException e )
86          {
87              return "Repo URL not valid";
88          }
89  
90  
91          //FIXME return real error message here
92          
93          Set<ConstraintViolation<FastDeployApplicationData>> errors = BeanValidationUtil.validate( fastDeployData );
94          if ( !errors.isEmpty() )
95          {
96          
97          	for ( ConstraintViolation<FastDeployApplicationData> constraint : errors )
98              {
99          	
100         		
101         		return constraint.getMessage();
102             }
103         	
104            
105         }
106 
107         return null;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
115     {
116         ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdHistory );
117         Demand demand = DemandHome.findByPrimaryKey( resourceHistory.getIdResource( ) );
118         // FIXME Load datasubset associated with this demand
119 
120         Map<String, Object> model = new HashMap<String, Object>( );
121         // model.put( MARK_HISTORY_LIST, listHistory );
122         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FAST_DEPLOY_DISPLAY_HISTORY, locale, model );
123         return template.getHtml( );
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public String getTaskInformationXml( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
131     {
132         return null;
133     }
134 }