datahub/web/app/dao/DatasetRowMapper.java

134 lines
4.9 KiB
Java
Raw Normal View History

2015-11-19 14:39:21 -08:00
/**
* Copyright 2015 LinkedIn Corp. All rights reserved.
*
* 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.
*/
package dao;
import models.Dataset;
2016-02-04 03:30:58 -08:00
import models.User;
2015-11-19 14:39:21 -08:00
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
2016-02-04 03:30:58 -08:00
import play.Logger;
2015-11-19 14:39:21 -08:00
import play.Play;
import java.sql.Time;
import java.sql.ResultSet;
import java.sql.SQLException;
2016-02-04 03:30:58 -08:00
import java.util.ArrayList;
import java.util.Arrays;
2015-11-19 14:39:21 -08:00
public class DatasetRowMapper implements RowMapper<Dataset>
{
public static String DATASET_ID_COLUMN = "id";
public static String DATASET_NAME_COLUMN = "name";
public static String DATASET_URN_COLUMN = "urn";
public static String DATASET_SOURCE_COLUMN = "source";
public static String DATASET_CREATED_TIME_COLUMN = "created";
public static String DATASET_MODIFIED_TIME_COLUMN = "modified";
2016-02-04 03:30:58 -08:00
public static String DATASET_SOURCE_MODIFIED_TIME_COLUMN = "source_modified_time";
2015-11-19 14:39:21 -08:00
public static String DATASET_PROPERTIES_COLUMN = "properties";
public static String DATASET_SCHEMA_COLUMN = "schema";
2016-02-04 03:30:58 -08:00
public static String DATASET_OWNER_ID_COLUMN = "owner_id";
public static String DATASET_OWNER_NAME_COLUMN = "owner_name";
public static String SCHEMA_HISTORY_ID_COLUMN = "schema_history_id";
2015-11-19 14:39:21 -08:00
public static String HDFS_PREFIX = "hdfs";
public static int HDFS_URN_PREFIX_LEN = 7; //for hdfs prefix is hdfs:///, but we need the last slash
2015-11-19 14:39:21 -08:00
@Override
public Dataset mapRow(ResultSet rs, int rowNum) throws SQLException
{
int id = rs.getInt(DATASET_ID_COLUMN);
String name = rs.getString(DATASET_NAME_COLUMN);
String urn = rs.getString(DATASET_URN_COLUMN);
String source = rs.getString(DATASET_SOURCE_COLUMN);
2016-02-04 03:30:58 -08:00
String strOwner = rs.getString(DATASET_OWNER_ID_COLUMN);
String strOwnerName = rs.getString(DATASET_OWNER_NAME_COLUMN);
2015-11-19 14:39:21 -08:00
Time created = rs.getTime(DATASET_CREATED_TIME_COLUMN);
Time modified = rs.getTime(DATASET_MODIFIED_TIME_COLUMN);
Integer schemaHistoryId = rs.getInt(SCHEMA_HISTORY_ID_COLUMN);
2016-02-04 03:30:58 -08:00
Long sourceModifiedTime = rs.getLong(DATASET_SOURCE_MODIFIED_TIME_COLUMN);
2015-11-19 14:39:21 -08:00
Dataset dataset = new Dataset();
dataset.id = id;
dataset.name = name;
dataset.urn = urn;
2016-02-04 03:30:58 -08:00
String[] owners = null;
if (StringUtils.isNotBlank(strOwner))
{
owners = strOwner.split(",");
}
String[] ownerNames = null;
if (StringUtils.isNotBlank(strOwnerName))
{
ownerNames = strOwnerName.split(",");
}
dataset.owners = new ArrayList<User>();
if (owners != null && ownerNames != null)
{
if (owners.length == ownerNames.length)
{
for (int i = 0; i < owners.length; i++)
{
User user = new User();
user.userName = owners[i];
if (StringUtils.isBlank(ownerNames[i]) || ownerNames[i].equalsIgnoreCase("*"))
{
user.name = owners[i];
}
else
{
user.name = ownerNames[i];
dataset.owners.add(user);
}
}
}
else
{
Logger.error("DatasetWithUserRowMapper get wrong owner and names. Dataset ID: "
+ Long.toString(dataset.id) + " Owner: " + owners + " Owner names: " + ownerNames);
}
}
2015-11-19 14:39:21 -08:00
if (StringUtils.isNotBlank(dataset.urn))
{
if (dataset.urn.substring(0, 4).equalsIgnoreCase(HDFS_PREFIX))
{
dataset.hdfsBrowserLink = Play.application().configuration().getString(DatasetsDAO.HDFS_BROWSER_URL_KEY) +
dataset.urn.substring(HDFS_URN_PREFIX_LEN);
2015-11-19 14:39:21 -08:00
}
}
dataset.source = source;
2016-02-04 03:30:58 -08:00
if (modified != null && sourceModifiedTime != null && sourceModifiedTime > 0)
2015-11-19 14:39:21 -08:00
{
dataset.modified = new java.util.Date(modified.getTime());
2016-02-04 03:30:58 -08:00
dataset.formatedModified = dataset.modified.toString();
2015-11-19 14:39:21 -08:00
}
if (created != null)
{
dataset.created = new java.util.Date(created.getTime());
} else if (modified != null) {
dataset.created = new java.util.Date(modified.getTime());
}
if (schemaHistoryId != null && schemaHistoryId > 0)
{
dataset.hasSchemaHistory = true;
}
else
{
dataset.hasSchemaHistory = false;
}
2015-11-19 14:39:21 -08:00
return dataset;
}
}