View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept.administration;
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 fr.ifremer.quadrige2.synchro.intercept.referential.AbstractReferentialInterceptor;
28  import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
29  import fr.ifremer.quadrige2.synchro.service.referential.ReferentialSynchroDatabaseConfiguration;
30  import org.apache.commons.collections4.CollectionUtils;
31  
32  import java.util.Set;
33  
34  /**
35   * Manage table on programs and strategies
36   * 
37   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
38   * @since 1.0
39   */
40  public abstract class AbstractProgramStrategyInterceptor extends AbstractReferentialInterceptor {
41  
42  	/**
43  	 * <p>
44  	 * Constructor for AbstractProgramStrategyInterceptor.
45  	 * </p>
46  	 */
47  	public AbstractProgramStrategyInterceptor() {
48  		super();
49  	}
50  
51  	/**
52  	 * <p>
53  	 * Constructor for AbstractProgramStrategyInterceptor.
54  	 * </p>
55  	 * 
56  	 * @param directions
57  	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
58  	 */
59  	public AbstractProgramStrategyInterceptor(SynchroDirection... directions) {
60  		super(directions);
61  	}
62  
63  	/**
64  	 * <p>
65  	 * Constructor for AbstractProgramStrategyInterceptor.
66  	 * </p>
67  	 * 
68  	 * @param tables
69  	 *            a {@link java.util.Set} object.
70  	 * @param directions
71  	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
72  	 */
73  	public AbstractProgramStrategyInterceptor(Set<String> tables, SynchroDirection... directions) {
74  		super(tables, directions);
75  	}
76  
77  	/**
78  	 * <p>
79  	 * Constructor for AbstractProgramStrategyInterceptor.
80  	 * </p>
81  	 * 
82  	 * @param tables
83  	 *            a {@link java.util.Set} object.
84  	 */
85  	public AbstractProgramStrategyInterceptor(Set<String> tables) {
86  		super(tables);
87  	}
88  
89  	/**
90  	 * <p>
91  	 * Constructor for AbstractProgramStrategyInterceptor.
92  	 * </p>
93  	 * 
94  	 * @param tableName
95  	 *            a {@link java.lang.String} object.
96  	 * @param directions
97  	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
98  	 */
99  	public AbstractProgramStrategyInterceptor(String tableName, SynchroDirection... directions) {
100 		super(tableName, directions);
101 	}
102 
103 	/**
104 	 * <p>
105 	 * Constructor for AbstractProgramStrategyInterceptor.
106 	 * </p>
107 	 * 
108 	 * @param tableName
109 	 *            a {@link java.lang.String} object.
110 	 */
111 	public AbstractProgramStrategyInterceptor(String tableName) {
112 		super(tableName);
113 	}
114 
115 	/* -- Internal methods -- */
116 
117 	/**
118 	 * <p>
119 	 * createProgramWhereClauseOrNull.
120 	 * </p>
121 	 * 
122 	 * @param config
123 	 *            a {@link fr.ifremer.quadrige2.synchro.service.referential.ReferentialSynchroDatabaseConfiguration}
124 	 *            object.
125 	 * @param finalExpression
126 	 *            a {@link java.lang.String} object.
127 	 * @return a {@link java.lang.String} object.
128 	 */
129 	protected String createProgramWhereClauseOrNull(ReferentialSynchroDatabaseConfiguration config, String finalExpression) {
130 		Set<String> programCodes = config.getProgramCodes();
131 
132 		// If no filter programs : nothing to do
133 		if (CollectionUtils.isEmpty(programCodes)) {
134 			return null;
135 		}
136 
137 		StringBuilder sb = new StringBuilder();
138 		sb.append('\'');
139 		Joiner.on("','").appendTo(sb, programCodes);
140 		sb.append('\'');
141 
142 		// Add restriction on PROG_CD
143 		return String.format(finalExpression,
144 				sb.toString());
145 	}
146 
147 }