/** * Copyright © 2013 enioka. All rights reserved * Authors: Marc-Antoine GOUILLART (marc-antoine.gouillart@enioka.com) * Pierre COPPEE (pierre.coppee@enioka.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.enioka.jqm.model; import java.io.Serializable; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import com.enioka.jqm.jdbc.DatabaseException; import com.enioka.jqm.jdbc.DbConn; import com.enioka.jqm.jdbc.NoResultException; import com.enioka.jqm.jdbc.QueryResult; /** * <strong>Not part of any API - this an internal JQM class and may change without notice.</strong> <br> * Persistence class for storing parameters related to the whole JQM cluster (parameters related to a single engine are stored inside * {@link Node}).<br> * Parameters are simple key/value string pairs. */ public class GlobalParameter implements Serializable { private static final long serialVersionUID = 2619971486012565203L; private Integer id; private String key; private String value; private Calendar lastModified; /** * The key (name) of the parameter. Most parameter keys are unique, but not all so there is no unique constraint inside the database. * This is the field used for retrieving {@link GlobalParameter}s.<br> * Max length is 50. */ public String getKey() { return key; } /** * See {@link #getKey()} */ public void setKey(String key) { this.key = key; } /** * Value of the parameter. Its interpretation depends on the key.<br> * Max length is 1000. */ public String getValue() { return value; } /** * See {@link #getValue()} */ public void setValue(String value) { this.value = value; } /** * A technical ID without any meaning. Generated by the database. */ public Integer getId() { return id; } /** * When the object was last modified. Read only. */ public Calendar getLastModified() { return lastModified; } /** * See {@link #getLastModified()} */ protected void setLastModified(Calendar lastModified) { this.lastModified = lastModified; } /** * Create a new GP entry in the database. No commit performed. */ public static GlobalParameter create(DbConn cnx, String key, String value) { QueryResult r = cnx.runUpdate("globalprm_insert", key, value); GlobalParameter res = new GlobalParameter(); res.id = r.getGeneratedId(); res.key = key; res.value = value; return res; } public static List<GlobalParameter> select(DbConn cnx, String query_key, Object... args) { List<GlobalParameter> res = new ArrayList<GlobalParameter>(); try { ResultSet rs = cnx.runSelect(query_key, args); while (rs.next()) { GlobalParameter tmp = new GlobalParameter(); tmp.id = rs.getInt(1); tmp.key = rs.getString(2); tmp.value = rs.getString(3); Calendar c = Calendar.getInstance(); c.setTimeInMillis(rs.getTimestamp(4).getTime()); tmp.lastModified = c; res.add(tmp); } } catch (SQLException e) { throw new DatabaseException(e); } return res; } /** * Retrieve the value of a single-valued parameter. * * @param key * @param defaultValue * @param cnx */ public static String getParameter(DbConn cnx, String key, String defaultValue) { try { return cnx.runSelectSingle("globalprm_select_by_key", 3, String.class, key); } catch (NoResultException e) { return defaultValue; } } public static void setParameter(DbConn cnx, String key, String value) { QueryResult qr = cnx.runUpdate("globalprm_update_value_by_key", value, key); if (qr.nbUpdated == 0) { create(cnx, key, value); } } }