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.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   * Implementation of {@link IDilaLocalCardService}
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      /** The serial ID */
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                 //if card was not added, we add it at the end
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 }