001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.search;
034:
035: import com.flexive.shared.FxContext;
036: import com.flexive.shared.FxLanguage;
037: import com.flexive.shared.FxSharedUtils;
038: import com.flexive.shared.FxFormatUtils;
039: import com.flexive.shared.exceptions.FxNotFoundException;
040: import com.flexive.shared.security.ACL;
041: import com.flexive.shared.security.UserTicket;
042:
043: import java.io.Serializable;
044: import java.util.ArrayList;
045: import java.util.List;
046: import java.util.Date;
047: import java.text.SimpleDateFormat;
048:
049: import org.apache.commons.lang.StringUtils;
050:
051: /**
052: * Search parameters
053: *
054: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
055: */
056: public class FxSQLSearchParams implements Serializable {
057: private static final long serialVersionUID = -3154811997979332790L;
058: private static final int DEFAULT_QUERY_TIMEOUT = 15; /*seconds*/
059:
060: /**
061: * The cache modes of the search.
062: * <p/>
063: * <li><b>OFF</b>: No caching will be used at all.</li>
064: * <li><b>ON</b>: The search will try to find the query result from its cache, and writes the result to the cache
065: * for the next similar querys to find it</li>
066: * <li><b>READ_ONLY</b>: The search will try to find the query result from its cache, but will
067: * not write the result to the cache</li>
068: */
069: public static enum CacheMode {
070: OFF(1), READ_ONLY(2), ON(3);
071: private int id;
072:
073: CacheMode(int id) {
074: this .id = id;
075: }
076:
077: /**
078: * Getter for the internal id
079: *
080: * @return internal id
081: */
082: public int getId() {
083: return id;
084: }
085:
086: /**
087: * Get a CacheMode by its id
088: *
089: * @param id the id
090: * @return CacheMode the type
091: * @throws com.flexive.shared.exceptions.FxNotFoundException
092: * if the mode does not exist
093: */
094: public static CacheMode getById(int id)
095: throws FxNotFoundException {
096: for (CacheMode mode : CacheMode.values()) {
097: if (mode.id == id)
098: return mode;
099: }
100: throw new FxNotFoundException(
101: "ex.sqlSearch.cacheMode.notFound.id", id);
102: }
103: }
104:
105: /**
106: * Envelope to carry data needed for a briefcase creation.
107: *
108: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
109: */
110: public static class BriefcaseCreationData implements Serializable {
111: private static final long serialVersionUID = -1050668347506270241L;
112: private final Long aclId;
113: private final String description;
114: private final String name;
115:
116: /**
117: * Constructor.
118: *
119: * @param name the name of the briefcase, in case of null or a empty String a name will be constructed
120: * @param description the description
121: * @param aclId the acl the briefcase is using, or null if the briefcase is not shared
122: */
123: public BriefcaseCreationData(String name, String description,
124: Long aclId) {
125: this .description = description == null ? "" : description;
126: if (StringUtils.isBlank(name)) {
127: final UserTicket ticket = FxContext.get().getTicket();
128: this .name = ticket.getUserName()
129: + "_"
130: + FxFormatUtils.getDateTimeFormat().format(
131: new Date());
132: } else {
133: this .name = name;
134: }
135: this .aclId = aclId;
136: }
137:
138: /**
139: * Returns the acl that the briefcase is using, or null if the briefcase is not shared.
140: *
141: * @return the aclId, or null if the briefcase is not shared
142: */
143: public Long getAclId() {
144: return aclId;
145: }
146:
147: /**
148: * The briefcase description (may be empty).
149: *
150: * @return the description
151: */
152: public String getDescription() {
153: return description;
154: }
155:
156: /**
157: * The briefcase name.
158: *
159: * @return the name
160: */
161: public String getName() {
162: return name;
163: }
164: }
165:
166: private BriefcaseCreationData bcd = null;
167: private int queryTimeout = -1;
168: private List<FxLanguage> resultLanguages;
169: private CacheMode cacheMode = CacheMode.OFF;
170: private static final CacheMode CACHE_MODE_DEFAULT = CacheMode.READ_ONLY;
171:
172: /**
173: * Constructor.
174: */
175: public FxSQLSearchParams() {
176: // empty
177: }
178:
179: /**
180: * Sets the caching mode for the search.
181: *
182: * @param mode the cache mode to use<br>
183: * CacheMode.ON: read from the cache if possible and write result to the cache<br>
184: * CacheMode.OFF: do not read or write the cache<br>
185: * CacheMode.READ_ONLY: read from the cache, but do not write to it<br>
186: * if null is specified CacheMode.READ_ONLY will be used.
187: * @return this
188: */
189: public FxSQLSearchParams setCacheMode(CacheMode mode) {
190: this .cacheMode = mode == null ? CACHE_MODE_DEFAULT : mode;
191: return this ;
192: }
193:
194: /**
195: * Returns the cache mode.
196: *
197: * @return the cache mode.
198: */
199: public CacheMode getCacheMode() {
200: return (cacheMode == null) ? CACHE_MODE_DEFAULT : cacheMode;
201: }
202:
203: /**
204: * Sets the languages that the resultset should contain.
205: *
206: * @param languages the languages, if null or a emtpty array is specified the default language of the
207: * calling user will be used.
208: * @return this
209: */
210: public FxSQLSearchParams setResultLanguages(
211: List<FxLanguage> languages) {
212: this .resultLanguages = languages;
213: return this ;
214: }
215:
216: /**
217: * Gets the languages that the resultset will contain.
218: *
219: * @return the languages
220: */
221: public List<FxLanguage> getResultLanguages() {
222: if (this .resultLanguages == null) {
223: this .resultLanguages = new ArrayList<FxLanguage>(1);
224: final UserTicket ticket = FxContext.get().getTicket();
225: this .resultLanguages.add(ticket.getLanguage());
226: }
227: return this .resultLanguages;
228: }
229:
230: /**
231: * Saves the result of the query in a new briefcase.
232: *
233: * @param name the name of the briefcase to create
234: * @param description the description of the briefcase
235: * @param aclId null if the briefcase is not shared, or a ACL to grant permissions to other users
236: * @return this
237: */
238: public FxSQLSearchParams saveResultInBriefcase(String name,
239: String description, Long aclId) {
240: this .bcd = new BriefcaseCreationData(name, description, aclId);
241: return this ;
242: }
243:
244: /**
245: * Saves the result of the query in a new briefcase.
246: *
247: * @param name the name of the briefcase to create
248: * @param description the description of the briefcase
249: * @param acl null if the briefcase is not shared, or a ACL to grant permissions to other users
250: * @return this
251: */
252: public FxSQLSearchParams saveResultInBriefcase(String name,
253: String description, ACL acl) {
254: Long aclId = acl == null ? null : acl.getId();
255: this .bcd = new BriefcaseCreationData(name, description, aclId);
256: return this ;
257: }
258:
259: /**
260: * Retuns true if the query will create a briefcase with the found objects.
261: *
262: * @return true if the query will create a briefcase with the found objects.
263: */
264: public boolean getWillCreateBriefcase() {
265: return (this .bcd != null);
266: }
267:
268: public BriefcaseCreationData getBriefcaseCreationData() {
269: return bcd;
270: }
271:
272: /**
273: * Sets the query timeout on the database.
274: *
275: * @param value in seconds, zero means unlimited
276: * @return this
277: */
278: public FxSQLSearchParams setQueryTimeout(int value) {
279: this .queryTimeout = value <= 0 ? -1 : value;
280: return this ;
281: }
282:
283: /**
284: * Returns the query timeout in seconds.
285: *
286: * @return the query timeout
287: */
288: public int getQueryTimeout() {
289: return (queryTimeout < 0) ? DEFAULT_QUERY_TIMEOUT
290: : queryTimeout;
291: }
292:
293: }
|