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.donneescomplementaires.dao.IComplementaryDataDAO;
37  import fr.paris.lutece.plugins.dila.business.donneescomplementaires.dto.ComplementaryDataDTO;
38  import fr.paris.lutece.plugins.dila.business.donneescomplementaires.dto.ComplementaryDataLinkDTO;
39  import fr.paris.lutece.plugins.dila.business.enums.ComplementaryLinkTypeEnum;
40  import fr.paris.lutece.plugins.dila.service.IDilaComplementaryDataLinkService;
41  import fr.paris.lutece.plugins.dila.service.IDilaComplementaryDataService;
42  
43  import org.apache.commons.collections.CollectionUtils;
44  import org.apache.commons.lang.StringUtils;
45  
46  import org.w3c.dom.Document;
47  import org.w3c.dom.Element;
48  
49  import java.io.Serializable;
50  
51  import java.util.List;
52  
53  import javax.inject.Inject;
54  import javax.inject.Named;
55  
56  import javax.xml.parsers.DocumentBuilder;
57  
58  
59  /**
60   * Implementation of {@link IDilaComplementaryDataService}
61   */
62  public class DilaComplementaryDataService implements IDilaComplementaryDataService, Serializable
63  {
64      private static final String TYPE_ATTRIBUTE = "type";
65      private static final String PROPERTY_TELESERVICE = "Téléservice";
66      private static final String TELESERVICE_TAG = "ServiceEnLigne";
67      private static final String TITLE_TAG = "Titre";
68      private static final String SEQ_ATTRIBUTE = "seq";
69      private static final String URL_ATTRIBUTE = "URL";
70      private static final String LEARN_MORE_TAG = "PourEnSavoirPlus";
71      private static final String COLUMN_BLOCK_TAG = "BlocColonne";
72      private static final String BOTTOM_BLOCK_TAG = "BlocBas";
73  
74      /** The serial ID */
75      private static final long serialVersionUID = 353073235359709773L;
76      @Inject
77      @Named( "dilaComplementaryDataDAO" )
78      private IComplementaryDataDAO _dilaComplementaryDataDAO;
79      @Inject
80      @Named( "dilaComplementaryDataLinkService" )
81      private IDilaComplementaryDataLinkService _dilaComplementaryDataLinkService;
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public List<ComplementaryDataDTO> findAll(  )
88      {
89          return _dilaComplementaryDataDAO.findAll(  );
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public Long create( ComplementaryDataDTO dto )
97      {
98          return _dilaComplementaryDataDAO.insert( dto );
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public boolean cardHasComplement( String id )
106     {
107         return _dilaComplementaryDataDAO.cardHasComplement( id );
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public ComplementaryDataDTO findById( Long id )
115     {
116         return _dilaComplementaryDataDAO.findById( id );
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void update( ComplementaryDataDTO complementaryData )
124     {
125         _dilaComplementaryDataDAO.store( complementaryData );
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public void delete( Long id )
133     {
134         _dilaComplementaryDataDAO.delete( id );
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public Document insertComplementaryData( Long cardId, Long categoryId, DocumentBuilder builder, Document document )
142     {
143         ComplementaryDataDTO complementaryDataDTO = this.findByCardAndAudience( cardId, categoryId );
144 
145         if ( complementaryDataDTO != null )
146         {
147             if ( StringUtils.isNotBlank( complementaryDataDTO.getBottomBlock(  ) ) )
148             {
149                 Element bottomBlock = document.createElement( BOTTOM_BLOCK_TAG );
150                 bottomBlock.setTextContent( complementaryDataDTO.getBottomBlock(  ) );
151                 document.getDocumentElement(  ).appendChild( bottomBlock );
152                 document.getDocumentElement(  ).normalize(  );
153             }
154 
155             if ( StringUtils.isNotBlank( complementaryDataDTO.getColumnBlock(  ) ) )
156             {
157                 Element columnBlock = document.createElement( COLUMN_BLOCK_TAG );
158                 columnBlock.setTextContent( complementaryDataDTO.getColumnBlock(  ) );
159                 document.getDocumentElement(  ).appendChild( columnBlock );
160                 document.getDocumentElement(  ).normalize(  );
161             }
162 
163             List<ComplementaryDataLinkDTO> listLearnMoreLinks = _dilaComplementaryDataLinkService.findByDataId( complementaryDataDTO.getId(  ),
164                     ComplementaryLinkTypeEnum.LEARN_MORE );
165 
166             if ( CollectionUtils.isNotEmpty( listLearnMoreLinks ) )
167             {
168                 for ( ComplementaryDataLinkDTO currentLink : listLearnMoreLinks )
169                 {
170                     Element currentElement = document.createElement( LEARN_MORE_TAG );
171                     currentElement.setAttribute( URL_ATTRIBUTE, currentLink.getURL(  ) );
172                     currentElement.setAttribute( SEQ_ATTRIBUTE, currentLink.getPosition(  ).toString(  ) );
173 
174                     Element currentTitle = document.createElement( TITLE_TAG );
175                     currentTitle.setTextContent( currentLink.getTitle(  ) );
176                     currentElement.appendChild( currentTitle );
177                     document.getDocumentElement(  ).appendChild( currentElement );
178                     document.getDocumentElement(  ).normalize(  );
179                 }
180             }
181 
182             List<ComplementaryDataLinkDTO> listTeleserviceLinks = _dilaComplementaryDataLinkService.findByDataId( complementaryDataDTO.getId(  ),
183                     ComplementaryLinkTypeEnum.TELESERVICE );
184 
185             if ( CollectionUtils.isNotEmpty( listTeleserviceLinks ) )
186             {
187                 for ( ComplementaryDataLinkDTO currentLink : listTeleserviceLinks )
188                 {
189                     Element currentElement = document.createElement( TELESERVICE_TAG );
190                     currentElement.setAttribute( URL_ATTRIBUTE, currentLink.getURL(  ) );
191                     currentElement.setAttribute( SEQ_ATTRIBUTE, currentLink.getPosition(  ).toString(  ) );
192                     currentElement.setAttribute( TYPE_ATTRIBUTE, PROPERTY_TELESERVICE );
193                     currentElement.setTextContent( currentLink.getTitle(  ) );
194                     document.getDocumentElement(  ).appendChild( currentElement );
195                     document.getDocumentElement(  ).normalize(  );
196                 }
197             }
198         }
199 
200         return document;
201     }
202 
203     /**
204      * {@inheritDoc}
205      */
206     @Override
207     public ComplementaryDataDTO findByCardAndAudience( Long ficheId, Long audienceId )
208     {
209         return _dilaComplementaryDataDAO.findByCardAndAudience( ficheId, audienceId );
210     }
211 }