// Copyright 2012 Citrix Systems, Inc. Licensed under the // Apache License, Version 2.0 (the "License"); you may not use this // file except in compliance with the License. Citrix Systems, Inc. // reserves all rights not expressly granted by 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. // // Automatically generated by addcopyright.py at 04/03/2012 package com.cloud.uuididentity.dao; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.UUID; import javax.ejb.Local; import org.apache.log4j.Logger; import com.cloud.api.IdentityMapper; import com.cloud.exception.InvalidParameterValueException; import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.Transaction; @Local(value={IdentityDao.class}) public class IdentityDaoImpl extends GenericDaoBase<IdentityVO, Long> implements IdentityDao { private static final Logger s_logger = Logger.getLogger(IdentityDaoImpl.class); public IdentityDaoImpl() { } @DB public Long getIdentityId(IdentityMapper mapper, String identityString) { assert(mapper.entityTableName() != null); return getIdentityId(mapper.entityTableName(), identityString); } @DB public Long getIdentityId(String tableName, String identityString) { assert(tableName != null); assert(identityString != null); PreparedStatement pstmt = null; Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { try { pstmt = txn.prepareAutoCloseStatement( String.format("SELECT id FROM `%s` WHERE id=? OR uuid=?", tableName) // TODO : after graceful period, use following line turn on more secure check // String.format("SELECT id FROM %s WHERE (id=? AND uuid IS NULL) OR uuid=?", mapper.entityTableName()) ); long id = 0; try { // TODO : use regular expression to determine id = Long.parseLong(identityString); } catch(NumberFormatException e) { // this could happen when it is a uuid string, so catch and ignore it } pstmt.setLong(1, id); pstmt.setString(2, identityString); ResultSet rs = pstmt.executeQuery(); if(rs.next()) { return rs.getLong(1); } else { if(id == -1L) return id; throw new InvalidParameterValueException("Object " + tableName + "(uuid: " + identityString + ") does not exist."); } } catch (SQLException e) { s_logger.error("Unexpected exception ", e); } } finally { txn.close(); } return null; } @DB public String getIdentityUuid(String tableName, String identityString) { assert(tableName != null); assert(identityString != null); PreparedStatement pstmt = null; Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { try { pstmt = txn.prepareAutoCloseStatement( String.format("SELECT uuid FROM `%s` WHERE id=? OR uuid=?", tableName) // String.format("SELECT uuid FROM %s WHERE (id=? AND uuid IS NULL) OR uuid=?", tableName) ); long id = 0; try { // TODO : use regular expression to determine id = Long.parseLong(identityString); } catch(NumberFormatException e) { // this could happen when it is a uuid string, so catch and ignore it } pstmt.setLong(1, id); pstmt.setString(2, identityString); ResultSet rs = pstmt.executeQuery(); if(rs.next()) { String uuid = rs.getString(1); if(uuid != null && !uuid.isEmpty()) return uuid; return identityString; } } catch (SQLException e) { s_logger.error("Unexpected exception ", e); } } finally { txn.close(); } return identityString; } @DB public void initializeDefaultUuid(String tableName) { assert(tableName != null); List<Long> l = getNullUuidRecords(tableName); Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { try { txn.start(); for(Long id : l) { setInitialUuid(tableName, id); } txn.commit(); } catch (SQLException e) { txn.rollback(); s_logger.error("Unexpected exception ", e); } } finally { txn.close(); } } @DB List<Long> getNullUuidRecords(String tableName) { List<Long> l = new ArrayList<Long>(); PreparedStatement pstmt = null; Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { try { pstmt = txn.prepareAutoCloseStatement( String.format("SELECT id FROM `%s` WHERE uuid IS NULL", tableName) ); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { l.add(rs.getLong(1)); } } catch (SQLException e) { s_logger.error("Unexpected exception ", e); } } finally { txn.close(); } return l; } @DB void setInitialUuid(String tableName, long id) throws SQLException { Transaction txn = Transaction.currentTxn(); PreparedStatement pstmtUpdate = null; pstmtUpdate = txn.prepareAutoCloseStatement( String.format("UPDATE `%s` SET uuid=? WHERE id=?", tableName) ); pstmtUpdate.setString(1, UUID.randomUUID().toString()); pstmtUpdate.setLong(2, id); pstmtUpdate.executeUpdate(); } }