View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.workflow.modules.formincrement.service;
35  
36  import fr.paris.lutece.plugins.directory.business.Record;
37  import fr.paris.lutece.plugins.directory.business.RecordHome;
38  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
39  import fr.paris.lutece.plugins.form.business.Form;
40  import fr.paris.lutece.plugins.form.business.FormHome;
41  import fr.paris.lutece.plugins.form.modules.exportdirectory.business.FormConfiguration;
42  import fr.paris.lutece.plugins.form.modules.exportdirectory.business.FormConfigurationHome;
43  import fr.paris.lutece.plugins.form.modules.exportdirectory.service.ExportdirectoryPlugin;
44  import fr.paris.lutece.plugins.form.service.FormPlugin;
45  import fr.paris.lutece.plugins.form.utils.FormUtils;
46  import fr.paris.lutece.plugins.workflow.modules.formincrement.business.TaskFormIncrementConfig;
47  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
48  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
49  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
50  import fr.paris.lutece.plugins.workflowcore.service.task.SimpleTask;
51  import fr.paris.lutece.portal.service.i18n.I18nService;
52  import fr.paris.lutece.portal.service.plugin.Plugin;
53  import fr.paris.lutece.portal.service.plugin.PluginService;
54  
55  import org.apache.commons.lang.StringUtils;
56  
57  import java.util.Collection;
58  import java.util.Locale;
59  
60  import javax.inject.Inject;
61  import javax.inject.Named;
62  
63  import javax.servlet.http.HttpServletRequest;
64  
65  
66  /**
67   *
68   * TaskFormIncrement
69   */
70  public class TaskFormIncrement extends SimpleTask
71  {
72      private static final String FIELD_INFORMATION_COMPLEMENTARY = "module.workflow.formincrement.task_form_increment_config.label_task_information";
73      private static final String MESSAGE_COMPLEMENTARY_INFORMATION_BEGIN = "module.workflow.formincrement.message.information_complementary.not_empty.begin";
74      private static final String MESSAGE_COMPLEMENTARY_INFORMATION_END = "module.workflow.formincrement.message.information_complementary.not_empty.end";
75      private static final String MESSAGE_METHOD_NOT_FOUND = "module.workflow.formincrement.message.information_complementary.method_not_found";
76      private static final String FORM_GETTER_INFORMATION_COMPLEMENTARY = "getInfoComplementary";
77      private static final String FORM_SETTER_INFORMATION_COMPLEMENTARY = "setInfoComplementary";
78      private static final String BEAN_TASK_CONFIG_SERVICE = "workflow-formincrement.taskFormIncrementConfigService";
79  
80      // SERVICES
81      @Inject
82      @Named( BEAN_TASK_CONFIG_SERVICE )
83      private ITaskConfigService _taskFormIncrementConfigService;
84      @Inject
85      private IResourceHistoryService _resourceHistoryService;
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
92      {
93          Plugin pluginForm = PluginService.getPlugin( FormPlugin.PLUGIN_NAME );
94          Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
95          Plugin pluginExportDirectory = PluginService.getPlugin( ExportdirectoryPlugin.PLUGIN_NAME );
96          TaskFormIncrementConfig config = _taskFormIncrementConfigService.findByPrimaryKey( this.getId(  ) );
97          ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
98  
99          if ( ( config != null ) && ( resourceHistory != null ) )
100         {
101             Record record = RecordHome.findByPrimaryKey( resourceHistory.getIdResource(  ), pluginDirectory );
102             int idForm = -1;
103             Collection<FormConfiguration> listExportDirectory = FormConfigurationHome.findAll( pluginExportDirectory );
104 
105             for ( FormConfiguration formConfiguration : listExportDirectory )
106             {
107                 if ( formConfiguration.getIdDirectory(  ) == record.getDirectory(  ).getIdDirectory(  ) )
108                 {
109                     idForm = formConfiguration.getIdForm(  );
110                 }
111             }
112 
113             Form form = FormHome.findByPrimaryKey( idForm, pluginForm );
114 
115             if ( form != null )
116             {
117                 try
118                 {
119                     String counter = (String) form.getClass(  )
120                                                   .getMethod( FORM_GETTER_INFORMATION_COMPLEMENTARY +
121                             config.getIdInformationComplementary(  ), (Class[]) null ).invoke( form, (Object[]) null );
122 
123                     try
124                     {
125                         if ( ( counter == null ) || counter.equals( "" ) )
126                         {
127                             counter = "1";
128                         }
129                         else
130                         {
131                             counter = String.valueOf( Integer.parseInt( counter ) + 1 );
132                         }
133 
134                         form.getClass(  )
135                             .getMethod( FORM_SETTER_INFORMATION_COMPLEMENTARY +
136                             config.getIdInformationComplementary(  ), String.class ).invoke( form, counter );
137                         FormHome.update( form, pluginForm );
138                     }
139                     catch ( Exception e )
140                     {
141                         throw new RuntimeException( I18nService.getLocalizedString( 
142                                 MESSAGE_COMPLEMENTARY_INFORMATION_BEGIN, locale ) +
143                             config.getIdInformationComplementary(  ) +
144                             I18nService.getLocalizedString( MESSAGE_COMPLEMENTARY_INFORMATION_END, locale ), e );
145                     }
146                 }
147                 catch ( Exception e )
148                 {
149                     throw new RuntimeException( I18nService.getLocalizedString( MESSAGE_METHOD_NOT_FOUND, locale ) );
150                 }
151             }
152         }
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public void doRemoveConfig(  )
160     {
161         _taskFormIncrementConfigService.remove( this.getId(  ) );
162     }
163 
164     /**
165      * {@inheritDoc}
166      */
167     @Override
168     public String getTitle( Locale locale )
169     {
170         TaskFormIncrementConfig config = _taskFormIncrementConfigService.findByPrimaryKey( this.getId(  ) );
171 
172         if ( config != null )
173         {
174             return I18nService.getLocalizedString( FIELD_INFORMATION_COMPLEMENTARY, locale ) +
175             config.getIdInformationComplementary(  );
176         }
177 
178         return StringUtils.EMPTY;
179     }
180 }