View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.service.history;
35  
36  import fr.paris.lutece.plugins.document.business.history.HistoryEvent;
37  import fr.paris.lutece.plugins.document.business.history.HistoryEventHome;
38  import fr.paris.lutece.plugins.document.service.DocumentEvent;
39  import fr.paris.lutece.plugins.document.service.DocumentEventListener;
40  import fr.paris.lutece.plugins.document.service.DocumentException;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  
43  import java.sql.Timestamp;
44  
45  
46  /**
47   * This service logs document changes history
48   */
49  public class DocumentHistoryService implements DocumentEventListener
50  {
51      private static final String PROPERTY_MESSAGE_DOCUMENT_CREATED = "document.history.message.documentCreated";
52      private static final String PROPERTY_MESSAGE_DOCUMENT_STATE_MODIFIED = "document.history.message.documentStateModified";
53      private static final String PROPERTY_MESSAGE_DOCUMENT_MODIFIED = "document.history.message.documentModified";
54      private static final String PROPERTY_MESSAGE_DOCUMENT_MOVED = "document.history.message.documentMoved";
55      private static DocumentHistoryServicee/history/DocumentHistoryService.html#DocumentHistoryService">DocumentHistoryService _singleton = new DocumentHistoryService(  );
56  
57      /** Creates a new instance of WorkflowEngine */
58      private DocumentHistoryService(  )
59      {
60      }
61  
62      /**
63       * Returns the unique instance of the service
64       * @return The unique instance of the service
65       */
66      public static DocumentHistoryService getInstance(  )
67      {
68          return _singleton;
69      }
70  
71      /**
72       * Process a document event
73       * @param event The event to process
74       * @throws DocumentException Exception occurs when error in event or rule
75       */
76      public void processDocumentEvent( DocumentEvent event )
77          throws DocumentException
78      {
79          try
80          {
81              HistoryEventt/business/history/HistoryEvent.html#HistoryEvent">HistoryEvent historyEvent = new HistoryEvent(  );
82              historyEvent.setDate( new Timestamp( new java.util.Date(  ).getTime(  ) ) );
83  
84              String strUser = ( event.getUser(  ) != null ) ? event.getUser(  ).getAccessCode(  ) : ""; // TODO : reconsider
85              historyEvent.setEventUser( strUser );
86              historyEvent.setIdDocument( event.getDocument(  ).getId(  ) );
87              historyEvent.setDocumentStateKey( event.getDocument(  ).getStateKey(  ) );
88              historyEvent.setSpace( event.getDocument(  ).getSpace(  ) );
89  
90              String strMessageKey;
91  
92              switch ( event.getEventType(  ) )
93              {
94                  case DocumentEvent.DOCUMENT_CREATED:
95                      strMessageKey = PROPERTY_MESSAGE_DOCUMENT_CREATED;
96  
97                      break;
98  
99                  case DocumentEvent.DOCUMENT_CONTENT_MODIFIED:
100                     strMessageKey = PROPERTY_MESSAGE_DOCUMENT_MODIFIED;
101 
102                     break;
103 
104                 case DocumentEvent.DOCUMENT_STATE_CHANGED:
105                     strMessageKey = PROPERTY_MESSAGE_DOCUMENT_STATE_MODIFIED;
106 
107                     break;
108 
109                 case DocumentEvent.DOCUMENT_MOVED:
110                     strMessageKey = PROPERTY_MESSAGE_DOCUMENT_MOVED;
111 
112                     break;
113 
114                 default:
115                     strMessageKey = PROPERTY_MESSAGE_DOCUMENT_MODIFIED;
116 
117                     break;
118             }
119 
120             historyEvent.setEventMessageKey( strMessageKey );
121             HistoryEventHome.create( historyEvent );
122         }
123         catch ( Exception e )
124         {
125             AppLogService.error( "Error in History even : " + e.getMessage(  ), e );
126         }
127     }
128 }