View Javadoc
1   /*
2    * Copyright (c) 2002-2019, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.appcenter.modules.sources.web;
35  
36  import java.io.IOException;
37  import java.util.HashMap;
38  import java.util.Locale;
39  import java.util.Map;
40  import java.util.Set;
41  
42  import javax.inject.Inject;
43  import javax.servlet.http.HttpServletRequest;
44  import javax.validation.ConstraintViolation;
45  
46  import fr.paris.lutece.plugins.appcenter.business.Application;
47  import fr.paris.lutece.plugins.appcenter.business.ApplicationHome;
48  import fr.paris.lutece.plugins.appcenter.business.DemandHome;
49  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesRightData;
50  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesRightDatas;
51  import fr.paris.lutece.plugins.appcenter.modules.sources.business.SourcesRightDemand;
52  import fr.paris.lutece.plugins.appcenter.modules.sources.service.SourcesUtil;
53  import fr.paris.lutece.plugins.appcenter.service.ApplicationService;
54  import fr.paris.lutece.plugins.appcenter.web.Constants;
55  import fr.paris.lutece.plugins.workflow.web.task.NoConfigTaskComponent;
56  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
57  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
58  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
59  import fr.paris.lutece.portal.service.template.AppTemplateService;
60  import fr.paris.lutece.portal.service.util.AppLogService;
61  import fr.paris.lutece.util.bean.BeanUtil;
62  import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
63  import fr.paris.lutece.util.html.HtmlTemplate;
64  
65  public class SourcesRightTaskComponent extends NoConfigTaskComponent
66  {
67      // TEMPLATES
68      private static final String TEMPLATE_SOURCES_RIGHT_TASK_FORM = "admin/plugins/appcenter/modules/sources/sources_right_task_form.html";
69      private static final String TEMPLATE_SOURCES_RIGHT_DISPLAY_HISTORY = "admin/plugins/appcenter/modules/sources/sources_right_task_history.html";
70  
71      @Inject
72      private IResourceHistoryService _resourceHistoryService;
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public String getDisplayTaskForm( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
79      {
80          Map<String, Object> model = new HashMap<String, Object>( );
81  
82          SourcesRightDemand demand = DemandHome.findByPrimaryKey( nIdResource, SourcesRightDemand.class );
83          if ( demand != null )
84          {
85              model.put( Constants.MARK_DEMAND, demand );
86              Application application = ApplicationHome.findByPrimaryKey( demand.getIdApplication( ) );
87              model.put( Constants.MARK_APPLICATION, application );
88  
89              try
90              {
91                  model.put( Constants.MARK_DATA, ApplicationService.loadApplicationDataById( demand.getIdApplicationData( ), application, SourcesRightDatas.class));
92              }
93              catch( IOException e )
94              {
95                 AppLogService.error( e );
96              }
97          }
98  
99          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SOURCES_RIGHT_TASK_FORM, locale, model );
100 
101         return template.getHtml( );
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public String doValidateTask( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
109     {
110         SourcesRightDataer/modules/sources/business/SourcesRightData.html#SourcesRightData">SourcesRightData sourcesData = new SourcesRightData( );
111         BeanUtil.populate( sourcesData, request );
112 
113         //FIXME return real error message here
114 
115         Set<ConstraintViolation<SourcesRightData>> errors = BeanValidationUtil.validate( sourcesData );
116         if ( !errors.isEmpty() )
117         {
118             for ( ConstraintViolation<SourcesRightData> constraint : errors )
119             {
120                 return constraint.getMessage();
121             }
122         }
123 
124         return null;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
132     {
133         ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdHistory );
134         SourcesRightDemand demand = DemandHome.findByPrimaryKey( resourceHistory.getIdResource( ),SourcesRightDemand.class );
135 
136         // FIXME Load datasubset associated with this demand
137         Map<String, Object> model = new HashMap<String, Object>( );
138          // model.put( MARK_HISTORY_LIST, listHistory );
139         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SOURCES_RIGHT_DISPLAY_HISTORY, locale, model );
140         return template.getHtml( );
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public String getTaskInformationXml( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
148     {
149         return null;
150     }
151 }