/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.airavata.sharing.registry.migrator.airavata;
import org.apache.airavata.common.exception.ApplicationSettingsException;
import org.apache.airavata.sharing.registry.models.*;
import org.apache.airavata.sharing.registry.server.SharingRegistryServerHandler;
import org.apache.thrift.TException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
public class AiravataDataMigrator {
public static void main(String[] args) throws SQLException, ClassNotFoundException, TException, ApplicationSettingsException {
Connection expCatConnection = ConnectionFactory.getInstance().getExpCatConnection();
SharingRegistryServerHandler sharingRegistryServerHandler = new SharingRegistryServerHandler();
String query = "SELECT * FROM GATEWAY";
Statement statement = expCatConnection.createStatement();
ResultSet rs = statement.executeQuery(query);
while (rs.next()) {
try{
//Creating domain entries
Domain domain = new Domain();
domain.setDomainId(rs.getString("GATEWAY_ID"));
domain.setName(rs.getString("GATEWAY_ID"));
domain.setDescription("Domain entry for " + domain.name);
if (!sharingRegistryServerHandler.isDomainExists(domain.domainId))
sharingRegistryServerHandler.createDomain(domain);
//Creating Entity Types for each domain
EntityType entityType = new EntityType();
entityType.setEntityTypeId(domain.domainId+":PROJECT");
entityType.setDomainId(domain.domainId);
entityType.setName("PROJECT");
entityType.setDescription("Project entity type");
if (!sharingRegistryServerHandler.isEntityTypeExists(entityType.domainId, entityType.entityTypeId))
sharingRegistryServerHandler.createEntityType(entityType);
entityType = new EntityType();
entityType.setEntityTypeId(domain.domainId+":EXPERIMENT");
entityType.setDomainId(domain.domainId);
entityType.setName("EXPERIMENT");
entityType.setDescription("Experiment entity type");
if (!sharingRegistryServerHandler.isEntityTypeExists(entityType.domainId, entityType.entityTypeId))
sharingRegistryServerHandler.createEntityType(entityType);
entityType = new EntityType();
entityType.setEntityTypeId(domain.domainId+":FILE");
entityType.setDomainId(domain.domainId);
entityType.setName("FILE");
entityType.setDescription("File entity type");
if (!sharingRegistryServerHandler.isEntityTypeExists(entityType.domainId, entityType.entityTypeId))
sharingRegistryServerHandler.createEntityType(entityType);
//Creating Permission Types for each domain
PermissionType permissionType = new PermissionType();
permissionType.setPermissionTypeId(domain.domainId+":READ");
permissionType.setDomainId(domain.domainId);
permissionType.setName("READ");
permissionType.setDescription("Read permission type");
if (!sharingRegistryServerHandler.isPermissionExists(permissionType.domainId, permissionType.permissionTypeId))
sharingRegistryServerHandler.createPermissionType(permissionType);
permissionType = new PermissionType();
permissionType.setPermissionTypeId(domain.domainId+":WRITE");
permissionType.setDomainId(domain.domainId);
permissionType.setName("WRITE");
permissionType.setDescription("Write permission type");
if (!sharingRegistryServerHandler.isPermissionExists(permissionType.domainId, permissionType.permissionTypeId))
sharingRegistryServerHandler.createPermissionType(permissionType);
}catch (Exception ex){
ex.printStackTrace();
}
}
//Creating user entries
query = "SELECT * FROM USERS";
statement = expCatConnection.createStatement();
rs = statement.executeQuery(query);
while(rs.next()){
try{
User user = new User();
user.setUserId(rs.getString("AIRAVATA_INTERNAL_USER_ID"));
user.setDomainId(rs.getString("GATEWAY_ID"));
user.setUserName(rs.getString("USER_NAME"));
if (!sharingRegistryServerHandler.isUserExists(user.domainId, user.userId))
sharingRegistryServerHandler.createUser(user);
}catch (Exception ex){
ex.printStackTrace();
}
}
//Creating project entries
query = "SELECT * FROM PROJECT";
statement = expCatConnection.createStatement();
rs = statement.executeQuery(query);
while(rs.next()){
try{
Entity entity = new Entity();
entity.setEntityId(rs.getString("PROJECT_ID"));
entity.setDomainId(rs.getString("GATEWAY_ID"));
entity.setEntityTypeId(rs.getString("GATEWAY_ID") + ":PROJECT");
entity.setOwnerId(rs.getString("USER_NAME") + "@" + rs.getString("GATEWAY_ID"));
entity.setName(rs.getString("PROJECT_NAME"));
entity.setDescription(rs.getString("DESCRIPTION"));
if(entity.getDescription() == null)
entity.setFullText(entity.getName());
else
entity.setFullText(entity.getName() + " " + entity.getDescription());
Map<String, String> metadata = new HashMap<>();
metadata.put("CREATION_TIME", rs.getDate("CREATION_TIME").toString());
if (!sharingRegistryServerHandler.isEntityExists(entity.domainId, entity.entityId))
sharingRegistryServerHandler.createEntity(entity);
}catch (Exception ex) {
ex.printStackTrace();
}
}
//Creating experiment entries
query = "SELECT * FROM EXPERIMENT";
statement = expCatConnection.createStatement();
rs = statement.executeQuery(query);
while(rs.next()){
try {
Entity entity = new Entity();
entity.setEntityId(rs.getString("EXPERIMENT_ID"));
entity.setDomainId(rs.getString("GATEWAY_ID"));
entity.setEntityTypeId(rs.getString("GATEWAY_ID") + ":EXPERIMENT");
entity.setOwnerId(rs.getString("USER_NAME") + "@" + rs.getString("GATEWAY_ID"));
entity.setParentEntityId(rs.getString("PROJECT_ID"));
entity.setName(rs.getString("EXPERIMENT_NAME"));
entity.setDescription(rs.getString("DESCRIPTION"));
if(entity.getDescription() == null)
entity.setFullText(entity.getName());
else
entity.setFullText(entity.getName() + " " + entity.getDescription());
Map<String, String> metadata = new HashMap<>();
metadata.put("CREATION_TIME", rs.getDate("CREATION_TIME").toString());
metadata.put("EXPERIMENT_TYPE", rs.getString("EXPERIMENT_TYPE"));
metadata.put("EXECUTION_ID", rs.getString("EXECUTION_ID"));
metadata.put("GATEWAY_EXECUTION_ID", rs.getString("GATEWAY_EXECUTION_ID"));
metadata.put("ENABLE_EMAIL_NOTIFICATION", rs.getString("ENABLE_EMAIL_NOTIFICATION"));
metadata.put("EMAIL_ADDRESSES", rs.getString("EMAIL_ADDRESSES"));
metadata.put("GATEWAY_INSTANCE_ID", rs.getString("GATEWAY_INSTANCE_ID"));
metadata.put("ARCHIVE", rs.getString("ARCHIVE"));
if (!sharingRegistryServerHandler.isEntityExists(entity.domainId, entity.entityId))
sharingRegistryServerHandler.createEntity(entity);
}catch (Exception ex){
ex.printStackTrace();
}
}
expCatConnection.close();
}
}