View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.referential;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Synchro Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.base.Joiner;
27  import com.google.common.base.Splitter;
28  import com.google.common.eventbus.Subscribe;
29  import fr.ifremer.common.synchro.meta.event.CreateQueryEvent;
30  import fr.ifremer.common.synchro.query.SynchroQueryBuilder;
31  import fr.ifremer.common.synchro.query.SynchroQueryOperator;
32  import fr.ifremer.quadrige2.core.config.Quadrige2Configuration;
33  import fr.ifremer.quadrige2.synchro.meta.DatabaseColumns;
34  import fr.ifremer.quadrige2.synchro.meta.referential.ReferentialSynchroTables;
35  import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
36  import org.apache.commons.lang3.StringUtils;
37  
38  /**
39   * Interceptor use to limit imported transcribing items, filter on transcribing item type
40   */
41  public class TranscribingItemInterceptor extends AbstractReferentialInterceptor {
42  
43      /**
44       * <p>
45       * Constructor for TranscribingItemInterceptor.
46       * </p>
47       */
48      public TranscribingItemInterceptor() {
49          super(ReferentialSynchroTables.TRANSCRIBING_ITEM.name(),
50                  SynchroDirection.IMPORT_SERVER2TEMP);
51      }
52  
53      /**
54       * <p>
55       * handleCreateQuery.
56       * </p>
57       *
58       * @param e a {@link fr.ifremer.common.synchro.meta.event.CreateQueryEvent} object.
59       */
60      @Subscribe
61      public void handleCreateQuery(CreateQueryEvent e) {
62  
63          String transcribingTypeLbIncludes = Quadrige2Configuration.getInstance().getImportTranscribingItemTypeLbIncludes();
64          StringBuilder protectedLabels = new StringBuilder();
65          if (StringUtils.isNotBlank(transcribingTypeLbIncludes)) {
66              protectedLabels.append('\'');
67              Joiner.on("','").appendTo(protectedLabels, Splitter.on(',').split(transcribingTypeLbIncludes));
68              protectedLabels.append('\'');
69          }
70  
71          switch (e.queryName) {
72              case count:
73              case countFromUpdateDate:
74              case select:
75              case selectFromUpdateDate:
76  
77                  // Add restriction on transcribing item type
78                  if (StringUtils.isNotBlank(protectedLabels.toString())) {
79  
80                      e.sql = SynchroQueryBuilder.newBuilder(e.sql)
81                              .addJoin(String.format("INNER JOIN %s tit ON t.%s = tit.%s",
82                                      ReferentialSynchroTables.TRANSCRIBING_ITEM_TYPE,
83                                      DatabaseColumns.TRANSC_ITEM_TYPE_ID,
84                                      DatabaseColumns.TRANSC_ITEM_TYPE_ID))
85                              .addWhere(SynchroQueryOperator.AND, String.format("tit.%s IN (%s)",
86                                      DatabaseColumns.TRANSC_ITEM_TYPE_LB,
87                                      protectedLabels.toString()))
88                              .build();
89                  }
90                  break;
91  
92              case selectMaxUpdateDate:
93  
94                  // Add specific query to determine the max update date.
95                  // It return the min update date against the max update date of each type, or 1970-01-01 if an item has not
96                  // been found
97                  if (StringUtils.isNotBlank(protectedLabels.toString())) {
98  
99                      // Add restriction on included ids
100                     e.sql = "SELECT MIN(COALESCE(UPDATE_DT, TO_TIMESTAMP('1970-01-01', 'YYYY-MM-DD'))) FROM (\n" +
101                             " SELECT TIT.TRANSC_ITEM_TYPE_ID, MAX(TI.UPDATE_DT) AS UPDATE_DT FROM TRANSCRIBING_ITEM_TYPE TIT\n" +
102                             " LEFT OUTER JOIN TRANSCRIBING_ITEM TI ON TI.TRANSC_ITEM_TYPE_ID=TIT.TRANSC_ITEM_TYPE_ID\n" +
103                             " WHERE TIT.TRANSC_ITEM_TYPE_LB IN (" + protectedLabels.toString() + ")\n" +
104                             " GROUP BY TIT.TRANSC_ITEM_TYPE_ID\n" +
105                             ")";
106                 }
107                 break;
108 
109             default:
110                 break;
111         }
112     }
113 }