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.ILocalCardDAO;
37 import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalCardDTO;
38 import fr.paris.lutece.plugins.dila.service.IDilaLocalCardService;
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 DilaLocalCardService implements IDilaLocalCardService, Serializable
57 {
58 private static final String SEQ_ATTRIBUTE = "seq";
59 private static final String ID_ATTRIBUTE = "ID";
60 private static final String CARD_TAG = "Fiche";
61
62
63 private static final long serialVersionUID = 3506441956169438006L;
64 @Inject
65 @Named( "dilaLocalCardDAO" )
66 private ILocalCardDAO _dilaLocalCardDAO;
67
68 @Override
69 public Long create( LocalCardDTO fiche )
70 {
71 return _dilaLocalCardDAO.insert( fiche );
72 }
73
74 @Override
75 public Long findCardIdByLocalId( String idLocal )
76 {
77 return _dilaLocalCardDAO.findCardIdByLocalId( idLocal );
78 }
79
80 @Override
81 public void delete( Long localCardId )
82 {
83 _dilaLocalCardDAO.delete( localCardId );
84 }
85
86 @Override
87 public LocalCardDTO findCardByLocalId( Long idLocal )
88 {
89 return _dilaLocalCardDAO.findCardByLocalId( idLocal );
90 }
91
92 @Override
93 public void update( LocalCardDTO card )
94 {
95 _dilaLocalCardDAO.store( card );
96 }
97
98 @Override
99 public boolean isCardWithParentId( String idLocal )
100 {
101 return _dilaLocalCardDAO.isCardWithParentId( idLocal );
102 }
103
104 @Override
105 public Document insertCardLinks( String parentId, DocumentBuilder builder, Document document )
106 {
107 List<LocalCardDTO> cardsList = this.findLocalCardsByParentFolder( parentId );
108
109 for ( LocalCardDTO currentCard : cardsList )
110 {
111 NodeList cards = document.getElementsByTagName( CARD_TAG );
112 Element newCard = document.createElement( CARD_TAG );
113 newCard.setAttribute( ID_ATTRIBUTE, currentCard.getLocalDTO( ).getId( ).toString( ) );
114 newCard.setTextContent( currentCard.getLocalDTO( ).getTitle( ) );
115
116 if ( StringUtils.isEmpty( currentCard.getSiblingCardId( ) ) )
117 {
118 if ( cards.getLength( ) > 0 )
119 {
120 Element cardElement = (Element) cards.item( cards.getLength( ) - 1 );
121 cardElement.getParentNode( ).appendChild( newCard );
122 }
123 else
124 {
125 document.appendChild( newCard );
126 }
127 }
128 else
129 {
130 boolean hasToShift = false;
131
132 for ( int i = 0; i < cards.getLength( ); i++ )
133 {
134 Element cardElement = (Element) cards.item( i );
135 String seq = cardElement.getAttribute( SEQ_ATTRIBUTE );
136
137 if ( cardElement.getAttribute( ID_ATTRIBUTE ).equals( currentCard.getSiblingCardId( ) ) )
138 {
139 if ( currentCard.getPosition( ) == 1 )
140 {
141 if ( seq != null )
142 {
143 newCard.setAttribute( SEQ_ATTRIBUTE, "" + Integer.parseInt( seq ) );
144 cardElement.setAttribute( SEQ_ATTRIBUTE, "" + ( Integer.parseInt( seq ) + 1 ) );
145 }
146
147 cardElement.getParentNode( ).insertBefore( newCard, cardElement );
148 }
149 else
150 {
151 if ( seq != null )
152 {
153 newCard.setAttribute( SEQ_ATTRIBUTE, "" + ( Integer.parseInt( seq ) + 1 ) );
154 }
155
156 cardElement.getParentNode( ).insertBefore( newCard, cardElement.getNextSibling( ) );
157 }
158
159 i++;
160 hasToShift = true;
161 }
162 else if ( hasToShift && ( seq != null ) )
163 {
164 cardElement.setAttribute( SEQ_ATTRIBUTE, "" + ( Integer.parseInt( seq ) + 1 ) );
165 }
166 }
167
168 if ( !hasToShift )
169 {
170 Element cardElement = (Element) cards.item( cards.getLength( ) - 1 );
171 cardElement.getParentNode( ).insertBefore( newCard, null );
172 }
173 }
174
175 document.getDocumentElement( ).normalize( );
176 }
177
178 return document;
179 }
180
181 @Override
182 public List<LocalCardDTO> findLocalCardsByParentFolder( String idParent )
183 {
184 return _dilaLocalCardDAO.findLocalCardsByParentFolder( idParent );
185 }
186 }