View Javadoc
1   package fr.paris.lutece.plugins.appcenter.modules.sources.web;
2   
3   import java.io.IOException;
4   import java.util.HashMap;
5   import java.util.Locale;
6   import java.util.Map;
7   import java.util.Set;
8   
9   import javax.inject.Inject;
10  import javax.servlet.http.HttpServletRequest;
11  import javax.validation.ConstraintViolation;
12  
13  
14  import fr.paris.lutece.plugins.appcenter.business.Application;
15  import fr.paris.lutece.plugins.appcenter.business.ApplicationHome;
16  import fr.paris.lutece.plugins.appcenter.business.Demand;
17  import fr.paris.lutece.plugins.appcenter.business.DemandHome;
18  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesData;
19  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesDatas;
20  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesDemand;
21  import fr.paris.lutece.plugins.appcenter.modules.sources.service.SourcesUtil;
22  import fr.paris.lutece.plugins.appcenter.service.ApplicationService;
23  import fr.paris.lutece.plugins.appcenter.web.Constants;
24  import fr.paris.lutece.plugins.workflow.web.task.NoConfigTaskComponent;
25  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
26  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
27  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
28  import fr.paris.lutece.portal.service.template.AppTemplateService;
29  import fr.paris.lutece.portal.service.util.AppLogService;
30  import fr.paris.lutece.util.ReferenceList;
31  import fr.paris.lutece.util.bean.BeanUtil;
32  import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
33  import fr.paris.lutece.util.html.HtmlTemplate;
34  
35  public class SourcesTaskComponent extends NoConfigTaskComponent
36  {
37      // TEMPLATES
38      private static final String TEMPLATE_SOURCES_TASK_FORM = "admin/plugins/appcenter/modules/sources/sources_task_form.html";
39      private static final String TEMPLATE_SOURCES_DISPLAY_HISTORY = "admin/plugins/appcenter/modules/sources/sources_task_history.html";
40     //MARKER
41      private static final String MARK_REPOSITORY_TYPES_MAP = "repository_types_map";
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          SourcesDemand demand = DemandHome.findByPrimaryKey( nIdResource, SourcesDemand.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             
60              try
61              {
62                  model.put( Constants.MARK_DATA,ApplicationService.loadApplicationDataById( demand.getIdApplicationData( ), application, SourcesDatas.class));
63              }
64              catch( IOException e )
65              {
66                 AppLogService.error( e );
67              }
68              
69              
70          }
71          model.put(SourcesUtil.MARK_REPOSITORY_TYPES_MAP, SourcesUtil.getAllRepositoryType(locale).toMap());
72          
73  
74          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SOURCES_TASK_FORM, locale, model );
75  
76          return template.getHtml( );
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public String doValidateTask( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
84      {
85          SourcesDatapcenter/modules/sources/business/SourcesData.html#SourcesData">SourcesData sourcesData = new SourcesData( );
86          BeanUtil.populate( sourcesData, request );
87          
88          
89          //FIXME return real error message here
90          
91          Set<ConstraintViolation<SourcesData>> errors = BeanValidationUtil.validate( sourcesData );
92          if ( !errors.isEmpty() )
93          {
94              for ( ConstraintViolation<SourcesData> constraint : errors )
95              {
96                  return constraint.getMessage();
97              }
98          }
99  
100         return null;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
108     {
109         ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdHistory );
110         SourcesDemand demand = DemandHome.findByPrimaryKey( resourceHistory.getIdResource( ),SourcesDemand.class );
111         // FIXME Load datasubset associated with this demand
112          Map<String, Object> model = new HashMap<String, Object>( );
113          model.put(SourcesUtil.MARK_REPOSITORY_TYPES_MAP, SourcesUtil.getAllRepositoryType(locale));
114          
115          // model.put( MARK_HISTORY_LIST, listHistory );
116         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SOURCES_DISPLAY_HISTORY, locale, model );
117         return template.getHtml( );
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public String getTaskInformationXml( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
125     {
126         return null;
127     }
128 }