View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of 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.participatoryideation.service.notify;
35  
36  import java.util.Locale;
37  
38  import javax.inject.Inject;
39  import javax.inject.Named;
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  import fr.paris.lutece.plugins.participatoryideation.business.notify.TaskChangeProposalStatusConfig;
45  import fr.paris.lutece.plugins.participatoryideation.business.proposal.Proposal;
46  import fr.paris.lutece.plugins.participatoryideation.business.proposal.ProposalHome;
47  import fr.paris.lutece.plugins.participatoryideation.service.ProposalService;
48  import fr.paris.lutece.plugins.participatoryideation.service.SolrProposalIndexer;
49  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
50  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
51  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
52  import fr.paris.lutece.plugins.workflowcore.service.task.SimpleTask;
53  import fr.paris.lutece.portal.service.i18n.I18nService;
54  import fr.paris.lutece.portal.service.spring.SpringContextService;
55  
56  /**
57   * 
58   * TaskChangeProposalStatus class
59   *
60   */
61  public class TaskChangeProposalStatus extends SimpleTask
62  {
63      /**
64       * Name of the bean of the config service of this task
65       */
66      public static final String CONFIG_SERVICE_BEAN_NAME = "participatoryideation.taskChangeProposalStatusConfigService";
67      // From plugin-ideation
68      private static final String BEAN_SOLR_PROPOSAL_INDEXER = "participatoryideation.solrProposalIndexer";
69  
70      // Messages
71      private static final String MESSAGE_UNPUBLISHED_PROPOSAL = "module.workflow.ideation.task_change_proposal_status.labelUnpublishedProposal";
72      private static final String MESSAGE_PUBLISHED_PROPOSAL = "module.workflow.ideation.task_change_proposal_status.labelPublishedProposal";
73      @Inject
74      private IResourceHistoryService _resourceHistoryService;
75      @Inject
76      @Named( CONFIG_SERVICE_BEAN_NAME )
77      private ITaskConfigService _taskChangeProposalStatusConfigService;
78  
79      @Override
80      public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
81      {
82          ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
83          TaskChangeProposalStatusConfig config = _taskChangeProposalStatusConfigService.findByPrimaryKey( this.getId( ) );
84  
85          if ( ( config != null ) && ( resourceHistory != null ) && Proposal.WORKFLOW_RESOURCE_TYPE.equals( resourceHistory.getResourceType( ) ) )
86          {
87              // We get the proposal to update
88              Proposal proposal = ProposalHome.findByPrimaryKey( resourceHistory.getIdResource( ) );
89  
90              if ( proposal != null )
91              {
92                  proposal.setStatusPublic( Proposal.Status.getByValue( config.getProposalStatus( ) ) );
93                  ProposalHome.updateBO( proposal );
94  
95                  SolrProposalIndexer solrProposalIndexer = SpringContextService.getBean( BEAN_SOLR_PROPOSAL_INDEXER );
96                  if ( ProposalService.getInstance( ).isPublished( proposal ) )
97                  {
98                      solrProposalIndexer.writeProposal( proposal );
99                  }
100                 else
101                 {
102                     solrProposalIndexer.removeProposal( proposal );
103                 }
104             }
105         }
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public String getTitle( Locale locale )
113     {
114         TaskChangeProposalStatusConfig config = _taskChangeProposalStatusConfigService.findByPrimaryKey( this.getId( ) );
115 
116         if ( ( config != null ) && ( config.getProposalStatus( ) != null ) )
117         {
118             return I18nService.getLocalizedString( Proposal.Status.getByValue( config.getProposalStatus( ) ).getLibelle( ), locale );
119         }
120 
121         return StringUtils.EMPTY;
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public void doRemoveConfig( )
129     {
130         _taskChangeProposalStatusConfigService.remove( this.getId( ) );
131     }
132 }