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.ILocalDAO;
37  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalDTO;
38  import fr.paris.lutece.plugins.dila.service.IDilaLocalService;
39  
40  import org.w3c.dom.Document;
41  import org.w3c.dom.Element;
42  
43  import java.io.Serializable;
44  
45  import java.util.List;
46  
47  import javax.inject.Inject;
48  import javax.inject.Named;
49  
50  import javax.xml.parsers.DocumentBuilder;
51  
52  
53  /**
54   * Implementation of {@link IDilaLocalService}
55   */
56  public class DilaLocalService implements IDilaLocalService, Serializable
57  {
58      private static final String ID_ATTRIBUTE = "ID";
59      private static final String CARD_TAG = "Fiche";
60  
61      /** The serial ID */
62      private static final long serialVersionUID = -9211044575927578839L;
63      @Inject
64      @Named( "dilaLocalDAO" )
65      private ILocalDAO _dilaLocalDAO;
66  
67      @Override
68      public LocalDTO findLocalByIdAndTypeAndAudience( Long id, Long type, Long idAudience )
69      {
70          return _dilaLocalDAO.findLocalByIdAndTypeAndAudience( id, type, idAudience );
71      }
72  
73      @Override
74      public String findTitleByIdAndTypeAndAudience( Long id, Long type, Long idAudience )
75      {
76          return _dilaLocalDAO.findTitleByIdAndTypeAndAudience( id, type, idAudience );
77      }
78  
79      @Override
80      public List<LocalDTO> findAll(  )
81      {
82          return _dilaLocalDAO.findAll(  );
83      }
84  
85      @Override
86      public Long create( LocalDTO local, boolean addIdToBreadcrumb )
87      {
88          return _dilaLocalDAO.insert( local, addIdToBreadcrumb );
89      }
90  
91      @Override
92      public void delete( String idLocal )
93      {
94          _dilaLocalDAO.delete( idLocal );
95      }
96  
97      @Override
98      public void update( LocalDTO localDTO, boolean addIdToBreadcrumb )
99      {
100         _dilaLocalDAO.store( localDTO, addIdToBreadcrumb );
101     }
102 
103     @Override
104     public String findXmlById( Long id )
105     {
106         return _dilaLocalDAO.findXmlById( id );
107     }
108 
109     @Override
110     public Document insertLastCardsLinks( Long categoryId, DocumentBuilder builder, Document document )
111     {
112         List<LocalDTO> localList = this.findLastCardsByAudience( categoryId );
113 
114         for ( LocalDTO currentLocal : localList )
115         {
116             Element newCard = document.createElement( CARD_TAG );
117             newCard.setAttribute( ID_ATTRIBUTE, currentLocal.getId(  ).toString(  ) );
118             newCard.setTextContent( currentLocal.getTitle(  ) );
119             document.getDocumentElement(  ).appendChild( newCard );
120             document.getDocumentElement(  ).normalize(  );
121         }
122 
123         return document;
124     }
125 
126     /**
127      * Return last locale cards by audience
128      * @param lAudienceId the audience to check
129      * @return the list of cards
130      */
131     private List<LocalDTO> findLastCardsByAudience( Long lAudienceId )
132     {
133         return _dilaLocalDAO.findLastCardsByAudience( lAudienceId );
134     }
135 
136     @Override
137     public List<LocalDTO> findAllByAudienceId( Long audienceId )
138     {
139         return _dilaLocalDAO.findAllByAudienceId( audienceId );
140     }
141 }