View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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.workflow.modules.ticketing.business.resourcehistory;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.inject.Inject;
41  
42  import fr.paris.lutece.plugins.ticketing.business.channel.Channel;
43  import fr.paris.lutece.plugins.ticketing.business.channel.ChannelHome;
44  import fr.paris.lutece.plugins.ticketing.business.resourcehistory.IResourceHistoryInformationService;
45  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
46  import fr.paris.lutece.portal.service.plugin.Plugin;
47  
48  /**
49   *
50   * ResourceHistoryService
51   *
52   */
53  public class ResourceHistoryService implements IResourceHistoryService, IResourceHistoryInformationService
54  {
55      /**
56       * The name of the bean of this service
57       */
58      public static final String BEAN_SERVICE = "workflow-ticketing.resourceHistoryService";
59      @Inject
60      private IResourceHistoryDAO _dao;
61      @Inject
62      private fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService _resourceHistoryService;
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public void create( ResourceHistory resourceHistory, Plugin plugin )
69      {
70          _dao.insert( resourceHistory, plugin );
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public void removeByHistory( int nIdHistory, Plugin plugin )
78      {
79          _dao.deleteByHistory( nIdHistory, plugin );
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public ResourceHistory findByPrimaryKey( int nIdHistory, Plugin plugin )
87      {
88          return _dao.load( nIdHistory, plugin );
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public void removeByResource( int nIdResource, String strResourceType )
96      {
97          _dao.deleteByResource( nIdResource, strResourceType, WorkflowUtils.getPlugin( ) );
98      }
99  
100     /**
101      * returns the list of id_history/channel associated to a ticket resource.
102      *
103      * @param nIdResource
104      *            the resource id
105      * @param strResourceType
106      *            the resource type
107      * @param nIdWorkflow
108      *            the workflow id
109      * @return the list of id_history/channel associated to a ticket resource
110      */
111     @Override
112     public Map<String, Channel> getChannelHistoryMap( int nIdResource, String strResourceType, int nIdWorkflow )
113     {
114         Map<String, Channel> mapHistoryChannel = new HashMap<>( );
115 
116         List<fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory> listResourceHistory = _resourceHistoryService
117                 .getAllHistoryByResource( nIdResource, strResourceType, nIdWorkflow );
118 
119         for ( fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory resourceHistory : listResourceHistory )
120         {
121             fr.paris.lutece.plugins.workflow.modules.ticketing.business.resourcehistory.ResourceHistory resourceHistoryChannel = findByPrimaryKey(
122                     resourceHistory.getId( ), WorkflowUtils.getPlugin( ) );
123 
124             if ( resourceHistoryChannel != null )
125             {
126                 Channel channel = ChannelHome.findByPrimaryKey( resourceHistoryChannel.getIdChannel( ) );
127                 mapHistoryChannel.put( Integer.toString( resourceHistoryChannel.getIdHistory( ) ), channel );
128             }
129         }
130 
131         return mapHistoryChannel;
132     }
133 
134     /**
135      * remove all the list of ticketing history resource associated to a ticket resource.
136      *
137      * @param nIdResource
138      *            the resource id
139      * @param strResourceType
140      *            the resource type
141      * @param nIdWorkflow
142      *            the workflow id
143      */
144     public void removeResourceHistory( int nIdResource, String strResourceType, int nIdWorkflow )
145     {
146         List<fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory> listResourceHistory = _resourceHistoryService
147                 .getAllHistoryByResource( nIdResource, strResourceType, nIdWorkflow );
148 
149         for ( fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory resourceHistory : listResourceHistory )
150         {
151             fr.paris.lutece.plugins.workflow.modules.ticketing.business.resourcehistory.ResourceHistory resourceHistoryTicketing = findByPrimaryKey(
152                     resourceHistory.getId( ), WorkflowUtils.getPlugin( ) );
153 
154             if ( resourceHistoryTicketing != null )
155             {
156                 removeByHistory( resourceHistory.getId( ), WorkflowUtils.getPlugin( ) );
157             }
158         }
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public ResourceHistory findOldUnitByPrimaryKey( int nIdHistory, Plugin plugin )
166     {
167         return _dao.loadUnitOld( nIdHistory, plugin );
168     }
169 
170 }