All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.jee.api.sql.util.UpsertGroup Maven / Gradle / Ivy

/*
 This is free and unencumbered software released into the public domain.

 Anyone is free to copy, modify, publish, use, compile, sell, or
 distribute this software, either in source code form or as a compiled
 binary, for any purpose, commercial or non-commercial, and by any
 means.

 In jurisdictions that recognize copyright laws, the author or authors
 of this software dedicate any and all copyright interest in the
 software to the public domain. We make this dedication for the benefit
 of the public at large and to the detriment of our heirs and
 successors. We intend this dedication to be an overt act of
 relinquishment in perpetuity of all present and future rights to this
 software under copyright law.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
package base.jee.api.sql.util;

import static base.jee.api.sql.util.Log.log;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import base.json.Json;
import base.security.ResourceUid;
import base.security.User;

public class UpsertGroup {

	public static UUID upsertGroup(Connection c, String group, int type, User updator) throws SQLException {
		PreparedStatement s = null;
		PreparedStatement t = null;
		ResultSet r = null;

		try {
			s = c.prepareStatement("select uuid, type from base_group where site=? and name=?");
			s.setString(1, updator.getSite());
			s.setString(2, group);
			r = s.executeQuery();
			if(!r.next()) {
				UUID uuid = new base.uuid.UUID().toJavaUUID();
				t = c.prepareStatement("insert into base_group (site, uuid, name, type) values(?,?,?,?)");
				t.setString(1, updator.getSite());
				t.setString(2, uuid.toString());
				t.setString(3, group);
				t.setInt(4, type);
				t.executeUpdate();
				log(c, "INFO", updator, "Added group \"" + Json.escape(group) + "\"", new ResourceUid("Person", updator.getPersonUuid().toString()), new ResourceUid("Group", uuid));
				return uuid;
			} else {
				String uuid = r.getString(1);
				if(type != r.getInt(2)) {
					t = c.prepareStatement("update base_group set type=? where site=? and uuid=?");
					t.setInt(1, type);
					t.setString(2, updator.getSite());
					t.setString(3, uuid.toString());
					t.executeUpdate();
					log(c, "INFO", updator, "Update group \"" + Json.escape(group) + "\"", new ResourceUid("Person", updator.getPersonUuid().toString()), new ResourceUid("Group", uuid));
				}
				return UUID.fromString(uuid);
			}

		} finally {
			if(r != null) { try { r.close(); } catch(SQLException e) {} }
			if(t != null) { try { t.close(); } catch(SQLException e) {} }
			if(s != null) { try { s.close(); } catch(SQLException e) {} }
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy