View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.directory.modules.multiview.business.record.column.querypart.impl;
35  
36  import java.util.Arrays;
37  import java.util.Iterator;
38  import java.util.LinkedHashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import org.apache.commons.lang3.StringUtils;
43  import org.apache.commons.lang3.math.NumberUtils;
44  
45  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.column.IRecordColumn;
46  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.column.impl.RecordColumnEntry;
47  import fr.paris.lutece.plugins.directory.modules.multiview.util.RecordEntryNameConstants;
48  import fr.paris.lutece.util.sql.DAOUtil;
49  
50  /**
51   * Implementation of the IRecordColumnQueryPart interface for an Entry column
52   */
53  public class RecordColumnEntryQueryPart extends AbstractRecordColumnQueryPart
54  {
55      // Constants
56      private static final String ENTRY_SELECT_QUERY_PART = "GROUP_CONCAT( DISTINCT column_%1$s.column_%1$s_value SEPARATOR ',') AS column_%1$s_value ";
57      private static final String ENTRY_FROM_QUERY_PART = StringUtils.EMPTY;
58      private static final String ENTRY_JOIN_SELECT_QUERY_PART = "LEFT JOIN ( SELECT record_%1$s.id_record AS id_record_%1$s, record_field_%1$s.record_field_value AS column_%1$s_value ";
59      private static final String ENTRY_JOIN_FROM_QUERY_PART = " FROM directory_record_field AS record_field_%s ";
60      private static final String ENTRY_JOIN_RECORD_QUERY_PART = " INNER JOIN directory_record AS record_%1$s ON record_field_%1$s.id_record = record_%1$s.id_record ";
61      private static final String ENTRY_JOIN_ENTRY_QUERY_PART = " INNER JOIN directory_entry AS entry_%1$s ON entry_%1$s.id_entry = record_field_%1$s.id_entry ";
62      private static final String ENTRY_JOIN_WHERE_QUERY_PART = " WHERE entry_%1$s.title IN ( %2$s ) ";
63      private static final String ENTRY_JOIN_QUERY_PART = " AS column_%1$s ON column_%1$s.id_record_%1$s = record.id_record";
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public String getRecordColumnSelectQuery( )
70      {
71          int nRecordColumnPosition = getRecordColumnPosition( );
72          String strRecordColumnSelectQuery = String.format( ENTRY_SELECT_QUERY_PART, nRecordColumnPosition );
73  
74          return strRecordColumnSelectQuery;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public String getRecordColumnFromQuery( )
82      {
83          return ENTRY_FROM_QUERY_PART;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public List<String> getRecordColumnJoinQueries( )
91      {
92          int nRecordColumnPosition = getRecordColumnPosition( );
93  
94          StringBuilder stringBuilderJoinQuery = new StringBuilder( );
95  
96          String strJoinSelectQueryPart = String.format( ENTRY_JOIN_SELECT_QUERY_PART, nRecordColumnPosition );
97          stringBuilderJoinQuery.append( strJoinSelectQueryPart );
98  
99          String strJoinFromQueryPart = String.format( ENTRY_JOIN_FROM_QUERY_PART, nRecordColumnPosition );
100         stringBuilderJoinQuery.append( strJoinFromQueryPart );
101 
102         String strJoinInnerJoinRecordQueryPart = String.format( ENTRY_JOIN_RECORD_QUERY_PART, nRecordColumnPosition );
103         stringBuilderJoinQuery.append( strJoinInnerJoinRecordQueryPart );
104 
105         String strJoinInnerJoinEntryQueryPart = String.format( ENTRY_JOIN_ENTRY_QUERY_PART, nRecordColumnPosition );
106         stringBuilderJoinQuery.append( strJoinInnerJoinEntryQueryPart );
107 
108         StringBuilder stringBuilderListEntryTitle = new StringBuilder( );
109         IRecordColumn recordColumn = getRecordColumn( );
110         if ( recordColumn instanceof RecordColumnEntry )
111         {
112             RecordColumnEntry recordColumnEntry = (RecordColumnEntry) recordColumn;
113             List<String> listEntryTitle = recordColumnEntry.getListEntryTitle( );
114             if ( listEntryTitle != null && !listEntryTitle.isEmpty( ) )
115             {
116                 buildListEntryTitle( stringBuilderListEntryTitle, listEntryTitle );
117             }
118         }
119 
120         String strJoinWhereQueryPart = String.format( ENTRY_JOIN_WHERE_QUERY_PART, nRecordColumnPosition, stringBuilderListEntryTitle.toString( ) );
121         stringBuilderJoinQuery.append( strJoinWhereQueryPart ).append( " ) " );
122 
123         String strJoinQueryPart = String.format( ENTRY_JOIN_QUERY_PART, nRecordColumnPosition );
124         stringBuilderJoinQuery.append( strJoinQueryPart );
125 
126         return Arrays.asList( stringBuilderJoinQuery.toString( ) );
127     }
128 
129     /**
130      * Build the list of all entry title from the given list quoting them and separate them by comma and after that append it to the given StringBuilder
131      * 
132      * @param stringBuilderListEntryTitle
133      *            The StringBuilder to append the list of all entry title
134      * @param listEntryTitle
135      *            The list of entry title to manage
136      */
137     private void buildListEntryTitle( StringBuilder stringBuilderListEntryTitle, List<String> listEntryTitle )
138     {
139         Iterator<String> iteratorListEntryTitle = listEntryTitle.iterator( );
140         while ( iteratorListEntryTitle.hasNext( ) )
141         {
142             String strEntryTitle = iteratorListEntryTitle.next( );
143             stringBuilderListEntryTitle.append( '\'' ).append( strEntryTitle ).append( '\'' );
144 
145             if ( iteratorListEntryTitle.hasNext( ) )
146             {
147                 stringBuilderListEntryTitle.append( ", " );
148             }
149         }
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     protected Map<String, Object> getMapRecordColumnValues( DAOUtil daoUtil )
157     {
158         int nRecordColumnPosition = getRecordColumnPosition( );
159         Map<String, Object> mapRecordColumnValues = new LinkedHashMap<>( );
160         String strEntryValueColumnName = String.format( RecordEntryNameConstants.COLUMN_ENTRY_VALUE_PATTERN, nRecordColumnPosition );
161         mapRecordColumnValues.put( strEntryValueColumnName, daoUtil.getString( strEntryValueColumnName ) );
162 
163         return mapRecordColumnValues;
164     }
165 
166     /**
167      * Return the position of the RecordColumn or NumberUtils.INTEGER_MINUS_ONE if doesn't exist
168      * 
169      * @return the position of the RecordColumn
170      */
171     private int getRecordColumnPosition( )
172     {
173         int nRecordColumnPosition = NumberUtils.INTEGER_MINUS_ONE;
174         IRecordColumn recordColumn = getRecordColumn( );
175         if ( recordColumn != null )
176         {
177             nRecordColumnPosition = recordColumn.getRecordColumnPosition( );
178         }
179 
180         return nRecordColumnPosition;
181     }
182 }