1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
64 private static final long serialVersionUID = -8207832026211506607L;
65 @Inject
66 @Named( "dilaLocalFolderDAO" )
67 private ILocalFolderDAO _dilaLocalFolderDAO;
68
69
70
71
72 @Override
73 public Long create( LocalFolderDTO dossier )
74 {
75 return _dilaLocalFolderDAO.create( dossier );
76 }
77
78
79
80
81 @Override
82 public Long findFolderIdByLocalId( String idLocal )
83 {
84 return _dilaLocalFolderDAO.findFolderIdByLocalId( idLocal );
85 }
86
87
88
89
90 @Override
91 public void delete( Long localFolderId )
92 {
93 _dilaLocalFolderDAO.delete( localFolderId );
94 }
95
96
97
98
99 @Override
100 public LocalFolderDTO findFolderByLocalId( Long idLocal )
101 {
102 return _dilaLocalFolderDAO.findFolderByLocalId( idLocal );
103 }
104
105
106
107
108 @Override
109 public void update( LocalFolderDTO folder )
110 {
111 _dilaLocalFolderDAO.store( folder );
112 }
113
114
115
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
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
184
185 @Override
186 public List<LocalFolderDTO> findLocalFoldersByParentTheme( String idParent )
187 {
188 return _dilaLocalFolderDAO.findLocalFoldersByParentTheme( idParent );
189 }
190 }