| 
									
										
										
										
											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 controllers;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import com.fasterxml.jackson.databind.JsonNode;
 | 
					
						
							|  |  |  | import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
					
						
							|  |  |  | import java.sql.SQLException;
 | 
					
						
							|  |  |  | import java.util.Map;
 | 
					
						
							| 
									
										
										
										
											2016-09-21 08:55:44 -07:00
										 |  |  | import java.util.List;
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | import models.daos.DatasetDao;
 | 
					
						
							| 
									
										
										
										
											2016-06-30 10:20:54 -07:00
										 |  |  | import models.daos.UserDao;
 | 
					
						
							| 
									
										
										
										
											2016-10-11 11:26:36 -07:00
										 |  |  | import utils.Urn;
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | import org.springframework.dao.EmptyResultDataAccessException;
 | 
					
						
							| 
									
										
										
										
											2016-06-14 16:17:24 -07:00
										 |  |  | import play.Logger;
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | import play.libs.Json;
 | 
					
						
							|  |  |  | import play.mvc.BodyParser;
 | 
					
						
							|  |  |  | import play.mvc.Controller;
 | 
					
						
							|  |  |  | import play.mvc.Result;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Created by zechen on 10/12/15.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							|  |  |  | public class DatasetController extends Controller {
 | 
					
						
							| 
									
										
										
										
											2016-06-30 10:20:54 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public static Result getDatasetWatchers(String datasetName)
 | 
					
						
							|  |  |  |       throws SQLException {
 | 
					
						
							|  |  |  |     ObjectNode resultJson = Json.newObject();
 | 
					
						
							|  |  |  |     if (datasetName != null) {
 | 
					
						
							|  |  |  |       ObjectNode result = UserDao.getWatchers(datasetName);
 | 
					
						
							|  |  |  |       resultJson.put("return_code", 200);
 | 
					
						
							| 
									
										
										
										
											2016-10-19 17:08:07 -07:00
										 |  |  |       resultJson.set("watchers", result);
 | 
					
						
							| 
									
										
										
										
											2016-06-30 10:20:54 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  |     return ok(resultJson);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |   public static Result getDatasetInfo() throws SQLException {
 | 
					
						
							|  |  |  |     ObjectNode resultJson = Json.newObject();
 | 
					
						
							|  |  |  |     String datasetIdString = request().getQueryString("datasetId");
 | 
					
						
							| 
									
										
										
										
											2016-10-19 17:08:07 -07:00
										 |  |  |     if (datasetIdString != null) {
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |       int datasetId = Integer.valueOf(datasetIdString);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       try {
 | 
					
						
							|  |  |  |         Map<String, Object> dataset = DatasetDao.getDatasetById(datasetId);
 | 
					
						
							|  |  |  |         resultJson.put("return_code", 200);
 | 
					
						
							|  |  |  |         resultJson.set("dataset", Json.toJson(dataset));
 | 
					
						
							|  |  |  |       } catch (EmptyResultDataAccessException e) {
 | 
					
						
							|  |  |  |         e.printStackTrace();
 | 
					
						
							|  |  |  |         resultJson.put("return_code", 404);
 | 
					
						
							|  |  |  |         resultJson.put("error_message", "dataset can not find!");
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  |       return ok(resultJson);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     String urn = request().getQueryString("urn");
 | 
					
						
							| 
									
										
										
										
											2016-10-19 17:08:07 -07:00
										 |  |  |     if (urn != null) {
 | 
					
						
							|  |  |  |       if (!Urn.validateUrn(urn)) {
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |         resultJson.put("return_code", 400);
 | 
					
						
							|  |  |  |         resultJson.put("error_message", "Urn format wrong!");
 | 
					
						
							|  |  |  |         return ok(resultJson);
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  |       try {
 | 
					
						
							|  |  |  |         Map<String, Object> dataset = DatasetDao.getDatasetByUrn(urn);
 | 
					
						
							|  |  |  |         resultJson.put("return_code", 200);
 | 
					
						
							|  |  |  |         resultJson.set("dataset", Json.toJson(dataset));
 | 
					
						
							|  |  |  |       } catch (EmptyResultDataAccessException e) {
 | 
					
						
							|  |  |  |         e.printStackTrace();
 | 
					
						
							|  |  |  |         resultJson.put("return_code", 404);
 | 
					
						
							|  |  |  |         resultJson.put("error_message", "dataset can not find!");
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  |       return ok(resultJson);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // if no parameter, return an error message
 | 
					
						
							|  |  |  |     resultJson.put("return_code", 400);
 | 
					
						
							|  |  |  |     resultJson.put("error_message", "No parameter provided");
 | 
					
						
							|  |  |  |     return ok(resultJson);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @BodyParser.Of(BodyParser.Json.class)
 | 
					
						
							|  |  |  |   public static Result addDataset() {
 | 
					
						
							|  |  |  |     JsonNode dataset = request().body().asJson();
 | 
					
						
							|  |  |  |     ObjectNode resultJson = Json.newObject();
 | 
					
						
							|  |  |  |     try {
 | 
					
						
							| 
									
										
										
										
											2016-07-21 10:38:36 -07:00
										 |  |  |       DatasetDao.setDatasetRecord(dataset);
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  |       resultJson.put("return_code", 200);
 | 
					
						
							|  |  |  |       resultJson.put("message", "Dataset inserted!");
 | 
					
						
							|  |  |  |     } catch (Exception e) {
 | 
					
						
							|  |  |  |       e.printStackTrace();
 | 
					
						
							|  |  |  |       resultJson.put("return_code", 404);
 | 
					
						
							|  |  |  |       resultJson.put("error_message", e.getMessage());
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ok(resultJson);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2016-06-14 16:17:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @BodyParser.Of(BodyParser.Json.class)
 | 
					
						
							|  |  |  |   public static Result getDatasetDependency() {
 | 
					
						
							|  |  |  |     String queryString = request().getQueryString("query");
 | 
					
						
							|  |  |  |     JsonNode input = Json.parse(queryString);
 | 
					
						
							|  |  |  |     ObjectNode resultJson = Json.newObject();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try {
 | 
					
						
							|  |  |  |       resultJson = DatasetDao.getDatasetDependency(input);
 | 
					
						
							|  |  |  |     } catch (Exception e) {
 | 
					
						
							|  |  |  |       Logger.error(e.getMessage());
 | 
					
						
							|  |  |  |       resultJson.put("return_code", 404);
 | 
					
						
							|  |  |  |       resultJson.put("error_message", e.getMessage());
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ok(resultJson);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2016-07-21 11:25:21 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |   public static Result getDatasetUrns(String propertiesLike)
 | 
					
						
							|  |  |  |       throws SQLException {
 | 
					
						
							|  |  |  |     ObjectNode resultJson = Json.newObject();
 | 
					
						
							|  |  |  |     try {
 | 
					
						
							|  |  |  |       if (propertiesLike != null) {
 | 
					
						
							|  |  |  |         ObjectNode result = DatasetDao.getDatasetUrnForPropertiesLike(propertiesLike);
 | 
					
						
							|  |  |  |         resultJson.put("return_code", 200);
 | 
					
						
							| 
									
										
										
										
											2016-10-19 17:08:07 -07:00
										 |  |  |         resultJson.set("dataset_urns", result);
 | 
					
						
							| 
									
										
										
										
											2016-07-21 11:25:21 +05:30
										 |  |  |       }
 | 
					
						
							|  |  |  |     } catch (Exception e) {
 | 
					
						
							|  |  |  |       e.printStackTrace();
 | 
					
						
							|  |  |  |       resultJson.put("return_code", 404);
 | 
					
						
							|  |  |  |       resultJson.put("error_message", e.getMessage());
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ok(resultJson);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2016-09-21 08:55:44 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public static Result getDatasetDependentsById(Long datasetId)
 | 
					
						
							| 
									
										
										
										
											2016-10-19 17:08:07 -07:00
										 |  |  |       throws SQLException {
 | 
					
						
							| 
									
										
										
										
											2016-09-21 08:55:44 -07:00
										 |  |  |     ObjectNode resultJson = Json.newObject();
 | 
					
						
							|  |  |  |     if (datasetId > 0) {
 | 
					
						
							|  |  |  |       try {
 | 
					
						
							|  |  |  |         List<Map<String, Object>> dependents = DatasetDao.getDatasetDependents(datasetId);
 | 
					
						
							|  |  |  |         resultJson.put("return_code", 200);
 | 
					
						
							|  |  |  |         resultJson.set("dependents", Json.toJson(dependents));
 | 
					
						
							|  |  |  |       } catch (EmptyResultDataAccessException e) {
 | 
					
						
							|  |  |  |         e.printStackTrace();
 | 
					
						
							|  |  |  |         resultJson.put("return_code", 404);
 | 
					
						
							|  |  |  |         resultJson.put("error_message", "no dependent datasets can be found!");
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  |       return ok(resultJson);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     // if no parameter, return an error message
 | 
					
						
							|  |  |  |     resultJson.put("return_code", 400);
 | 
					
						
							|  |  |  |     resultJson.put("error_message", "Dataset Id is not provided or invalid");
 | 
					
						
							|  |  |  |     return ok(resultJson);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public static Result getDatasetDependentsByUri(String datasetUri)
 | 
					
						
							| 
									
										
										
										
											2016-10-19 17:08:07 -07:00
										 |  |  |       throws SQLException {
 | 
					
						
							| 
									
										
										
										
											2016-09-21 08:55:44 -07:00
										 |  |  |     /* expect
 | 
					
						
							|  |  |  |      * hive:///db_name.table_name
 | 
					
						
							|  |  |  |      * hive:///db_name/table_name
 | 
					
						
							|  |  |  |      * dalids:///db_name.table_name
 | 
					
						
							|  |  |  |      * dalids:///db_name/table_name
 | 
					
						
							|  |  |  |      * hdfs:///dir1/dir2/dir3/dir4
 | 
					
						
							|  |  |  |      * teradata:///db_name/table_name
 | 
					
						
							|  |  |  |      */
 | 
					
						
							|  |  |  |     ObjectNode resultJson = Json.newObject();
 | 
					
						
							|  |  |  |     String[] uri_parts = datasetUri.split(":");
 | 
					
						
							| 
									
										
										
										
											2016-10-19 17:08:07 -07:00
										 |  |  |     if (uri_parts.length != 2) {
 | 
					
						
							| 
									
										
										
										
											2016-09-21 08:55:44 -07:00
										 |  |  |       resultJson.put("return_code", 400);
 | 
					
						
							|  |  |  |       resultJson.put("error_message", "Invalid dataset URI");
 | 
					
						
							|  |  |  |       return ok(resultJson);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     String dataset_type = uri_parts[0];
 | 
					
						
							|  |  |  |     String dataset_path = uri_parts[1].substring(2);  // start from the 3rd slash
 | 
					
						
							| 
									
										
										
										
											2016-10-19 17:08:07 -07:00
										 |  |  |     if (dataset_path.indexOf(".") > 0) {
 | 
					
						
							| 
									
										
										
										
											2016-09-21 08:55:44 -07:00
										 |  |  |       dataset_path.replace(".", "/");
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (dataset_path != null) {
 | 
					
						
							|  |  |  |       try {
 | 
					
						
							|  |  |  |         List<Map<String, Object>> dependents = DatasetDao.getDatasetDependents(dataset_type, dataset_path);
 | 
					
						
							|  |  |  |         resultJson.put("return_code", 200);
 | 
					
						
							|  |  |  |         resultJson.set("dependents", Json.toJson(dependents));
 | 
					
						
							|  |  |  |       } catch (EmptyResultDataAccessException e) {
 | 
					
						
							|  |  |  |         e.printStackTrace();
 | 
					
						
							|  |  |  |         resultJson.put("return_code", 404);
 | 
					
						
							|  |  |  |         resultJson.put("error_message", "No dependent dataset can be found!");
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  |       return ok(resultJson);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // if no parameter, return an error message
 | 
					
						
							|  |  |  |     resultJson.put("return_code", 400);
 | 
					
						
							|  |  |  |     resultJson.put("error_message", "No parameter provided");
 | 
					
						
							|  |  |  |     return ok(resultJson);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2015-11-19 14:39:21 -08:00
										 |  |  | }
 |