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.dila.service.impl;
35  
36  import fr.paris.lutece.plugins.dila.business.fichelocale.dao.ILocalFolderDAO;
37  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalFolderDTO;
38  import fr.paris.lutece.plugins.dila.service.IDilaLocalFolderService;
39  
40  import java.io.Serializable;
41  import java.util.List;
42  
43  import javax.inject.Inject;
44  import javax.inject.Named;
45  import javax.xml.parsers.DocumentBuilder;
46  
47  import org.apache.commons.lang.StringUtils;
48  import org.w3c.dom.Document;
49  import org.w3c.dom.Element;
50  import org.w3c.dom.NodeList;
51  
52  
53  /**
54   * Implementation of {@link IDilaLocalFolderService}
55   */
56  public class DilaLocalFolderService implements IDilaLocalFolderService, Serializable
57  {
58      private static final String SEQ_ATTRIBUTE = "seq";
59      private static final String TITLE_TAG = "Titre";
60      private static final String ID_ATTRIBUTE = "ID";
61      private static final String FOLDER_TAG = "Dossier";
62  
63      /** The serial ID */
64      private static final long serialVersionUID = -8207832026211506607L;
65      @Inject
66      @Named( "dilaLocalFolderDAO" )
67      private ILocalFolderDAO _dilaLocalFolderDAO;
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public Long create( LocalFolderDTO dossier )
74      {
75          return _dilaLocalFolderDAO.create( dossier );
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public Long findFolderIdByLocalId( String idLocal )
83      {
84          return _dilaLocalFolderDAO.findFolderIdByLocalId( idLocal );
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public void delete( Long localFolderId )
92      {
93          _dilaLocalFolderDAO.delete( localFolderId );
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public LocalFolderDTO findFolderByLocalId( Long idLocal )
101     {
102         return _dilaLocalFolderDAO.findFolderByLocalId( idLocal );
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public void update( LocalFolderDTO folder )
110     {
111         _dilaLocalFolderDAO.store( folder );
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public Document insertFolderLinks( String parentId, DocumentBuilder builder, Document document )
119     {
120         List<LocalFolderDTO> folderList = this.findLocalFoldersByParentTheme( parentId );
121 
122         for ( LocalFolderDTO currentFolder : folderList )
123         {
124             NodeList folders = document.getElementsByTagName( FOLDER_TAG );
125             Element newFolder = document.createElement( FOLDER_TAG );
126             newFolder.setAttribute( ID_ATTRIBUTE, currentFolder.getLocalDTO( ).getId( ).toString( ) );
127 
128             Element newTitle = document.createElement( TITLE_TAG );
129             newTitle.setTextContent( currentFolder.getLocalDTO( ).getTitle( ) );
130             newFolder.appendChild( newTitle );
131 
132             boolean hasToShift = false;
133 
134             for ( int i = 0; i < folders.getLength( ); i++ )
135             {
136                 Element folderElement = (Element) folders.item( i );
137                 String seq = folderElement.getAttribute( SEQ_ATTRIBUTE );
138 
139                 if ( folderElement.getAttribute( ID_ATTRIBUTE ).equals( currentFolder.getSiblingFolderId( ) ) )
140                 {
141                     if ( currentFolder.getPosition( ) == 1 )
142                     {
143                         if ( StringUtils.isNotBlank( seq ) )
144                         {
145                             newFolder.setAttribute( SEQ_ATTRIBUTE, "" + Integer.parseInt( seq ) );
146                             folderElement.setAttribute( SEQ_ATTRIBUTE, "" + ( Integer.parseInt( seq ) + 1 ) );
147                         }
148 
149                         folderElement.getParentNode( ).insertBefore( newFolder, folderElement );
150                     }
151                     else
152                     {
153                         if ( StringUtils.isNotBlank( seq ) )
154                         {
155                             newFolder.setAttribute( SEQ_ATTRIBUTE, "" + ( Integer.parseInt( seq ) + 1 ) );
156                         }
157 
158                         folderElement.getParentNode( ).insertBefore( newFolder, folderElement.getNextSibling( ) );
159                     }
160 
161                     i++;
162                     hasToShift = true;
163                 }
164                 else if ( hasToShift && ( StringUtils.isNotBlank( seq ) ) )
165                 {
166                     folderElement.setAttribute( SEQ_ATTRIBUTE, "" + ( Integer.parseInt( seq ) + 1 ) );
167                 }
168             }
169             //if folder was not added, we add it at the end
170             if ( !hasToShift )
171             {
172                 Element folderElement = (Element) folders.item( folders.getLength( ) - 1 );
173                 folderElement.getParentNode( ).insertBefore( newFolder, null );
174             }
175 
176             document.getDocumentElement( ).normalize( );
177         }
178 
179         return document;
180     }
181 
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public List<LocalFolderDTO> findLocalFoldersByParentTheme( String idParent )
187     {
188         return _dilaLocalFolderDAO.findLocalFoldersByParentTheme( idParent );
189     }
190 }