This is an automated email from the ASF dual-hosted git repository.

oleewere pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ambari-logsearch.git


The following commit(s) were added to refs/heads/master by this push:
     new 864fcc8  AMBARI-24939. ADDENDUM - add /list for metadata resource
864fcc8 is described below

commit 864fcc8c9fb7b985ec1955c7a963858716ca6e48
Author: Oliver Szabo <oleew...@gmail.com>
AuthorDate: Wed Nov 28 14:33:51 2018 +0100

    AMBARI-24939. ADDENDUM - add /list for metadata resource
---
 .../apache/ambari/logsearch/doc/DocConstants.java  |  5 ++-
 .../ambari/logsearch/manager/MetadataManager.java  | 47 +++++++++++++++++++---
 .../ambari/logsearch/rest/MetadataResource.java    | 36 ++++++++++++++---
 3 files changed, 76 insertions(+), 12 deletions(-)

diff --git 
a/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/doc/DocConstants.java
 
b/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/doc/DocConstants.java
index 21eb18f..8301bb0 100644
--- 
a/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/doc/DocConstants.java
+++ 
b/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/doc/DocConstants.java
@@ -120,9 +120,12 @@ public class DocConstants {
   }
 
   public class MetadataOperationDescriptions {
+    public static final String GET_METADATA_OD = "Get metadata";
+    public static final String GET_METADATA_LIST_OD = "Get metadata list";
     public static final String SAVE_METADATA_OD = "Save metadata";
+    public static final String SAVE_METADATA_LIST_OD = "Save metadata list";
     public static final String DELETE_METADATA_OD = "Delete metadata";
-    public static final String GET_METADATA_OD = "Get metadata";
+    public static final String DELETE_METADATA_LIST_OD = "Delete metadata 
list";
   }
 
   public class ShipperConfigOperationDescriptions {
diff --git 
a/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/manager/MetadataManager.java
 
b/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/manager/MetadataManager.java
index 3211b45..a0be60f 100644
--- 
a/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/manager/MetadataManager.java
+++ 
b/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/manager/MetadataManager.java
@@ -21,13 +21,16 @@ package org.apache.ambari.logsearch.manager;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 import java.util.UUID;
 
 import org.apache.ambari.logsearch.common.LogSearchContext;
 import org.apache.ambari.logsearch.dao.MetadataSolrDao;
 import org.apache.ambari.logsearch.model.request.impl.MetadataRequest;
+import 
org.apache.ambari.logsearch.model.request.impl.query.MetadataQueryRequest;
 import org.apache.ambari.logsearch.model.response.LogsearchMetaData;
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang3.builder.ToStringBuilder;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.solr.client.solrj.SolrQuery;
@@ -55,12 +58,24 @@ public class MetadataManager extends JsonManagerBase {
   @Inject
   private ConversionService conversionService;
 
+  public String saveMetadata(Collection<LogsearchMetaData> 
logsearchMetaDataList) {
+    List<SolrInputDocument> solrInputDocList = new ArrayList<>();
+    for (LogsearchMetaData metaData : logsearchMetaDataList) {
+      solrInputDocList.add(saveOneMetadata(metaData));
+    }
+    return convertObjToString(solrInputDocList);
+  }
+
   public String saveMetadata(LogsearchMetaData metadata) {
+    return convertObjToString(saveOneMetadata(metadata));
+  }
+
+  private SolrInputDocument saveOneMetadata(LogsearchMetaData metadata) {
     String name = metadata.getName();
 
     SolrInputDocument solrInputDoc = new SolrInputDocument();
     if (!isValid(metadata, false)) {
-      throw new MalformedInputException("Name,type and value should be 
specified");
+      throw new MalformedInputException("Name,type and value should be 
specified, input:" + ToStringBuilder.reflectionToString(metadata));
     }
     final String userName = 
LogSearchContext.getCurrentUsername().toLowerCase();
     solrInputDoc.addField(ID, generateUniqueId(metadata, userName));
@@ -70,12 +85,18 @@ public class MetadataManager extends JsonManagerBase {
     solrInputDoc.addField(TYPE, metadata.getType());
 
     metadataSolrDao.addDocs(solrInputDoc);
-    return convertObjToString(solrInputDoc);
+    return solrInputDoc;
+  }
+
+  public void deleteMetadata(Collection<LogsearchMetaData> metaDataList) {
+    for (LogsearchMetaData metaData : metaDataList) {
+      deleteMetadata(metaData);
+    }
   }
 
   public void deleteMetadata(LogsearchMetaData metaData) {
     if (!isValid(metaData, true)) {
-      throw new MalformedInputException("Name and type should be specified");
+      throw new MalformedInputException("Name and type should be specified, 
input: " + ToStringBuilder.reflectionToString(metaData));
     }
     final String userName;
     if (StringUtils.isNotBlank(metaData.getUserName())) {
@@ -86,8 +107,21 @@ public class MetadataManager extends JsonManagerBase {
     metadataSolrDao.deleteMetadata(metaData.getName(), metaData.getType(), 
userName);
   }
 
+  public LogsearchMetaData getMetadata(MetadataRequest request) {
+    Collection<LogsearchMetaData> metadataList = getMetadataObjects(request, 
true);
+    if (metadataList.isEmpty()) {
+      throw new NotFoundException("Not found any metadata objects with these 
inputs.");
+    } else {
+      return metadataList.iterator().next();
+    }
+  }
+
+  public Collection<LogsearchMetaData> getMetadataList(MetadataRequest 
request) {
+    return getMetadataObjects(request, false);
+  }
+
   @SuppressWarnings("unchecked")
-  public Collection<LogsearchMetaData> getMetadata(MetadataRequest request) {
+  private Collection<LogsearchMetaData> getMetadataObjects(MetadataRequest 
request, boolean requireNameField) {
     SolrQuery metadataQueryQuery = conversionService.convert(request, 
SolrQuery.class);
 
     final String userName;
@@ -99,6 +133,9 @@ public class MetadataManager extends JsonManagerBase {
     if (StringUtils.isBlank(userName)) {
       throw new IllegalArgumentException("User name is not found for metadata 
request.");
     }
+    if (requireNameField && StringUtils.isBlank(request.getName())) {
+      throw new MalformedInputException("Name field is a required request 
parameter.");
+    }
     metadataQueryQuery.addFilterQuery(String.format("%s:%s", USER_NAME, 
userName.toLowerCase()));
     SolrDocumentList solrList = 
metadataSolrDao.process(metadataQueryQuery).getResults();
 
@@ -114,9 +151,7 @@ public class MetadataManager extends JsonManagerBase {
 
       metadataList.add(metadata);
     }
-
     return metadataList;
-
   }
 
   private String generateUniqueId(LogsearchMetaData metaData, String userName) 
{
diff --git 
a/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/rest/MetadataResource.java
 
b/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/rest/MetadataResource.java
index c0f256c..c91abbc 100644
--- 
a/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/rest/MetadataResource.java
+++ 
b/ambari-logsearch-server/src/main/java/org/apache/ambari/logsearch/rest/MetadataResource.java
@@ -20,7 +20,10 @@
 package org.apache.ambari.logsearch.rest;
 
 import static 
org.apache.ambari.logsearch.doc.DocConstants.MetadataOperationDescriptions.DELETE_METADATA_OD;
+import static 
org.apache.ambari.logsearch.doc.DocConstants.MetadataOperationDescriptions.DELETE_METADATA_LIST_OD;
 import static 
org.apache.ambari.logsearch.doc.DocConstants.MetadataOperationDescriptions.GET_METADATA_OD;
+import static 
org.apache.ambari.logsearch.doc.DocConstants.MetadataOperationDescriptions.GET_METADATA_LIST_OD;
+import static 
org.apache.ambari.logsearch.doc.DocConstants.MetadataOperationDescriptions.SAVE_METADATA_LIST_OD;
 import static 
org.apache.ambari.logsearch.doc.DocConstants.MetadataOperationDescriptions.SAVE_METADATA_OD;
 
 import java.util.Collection;
@@ -54,6 +57,21 @@ public class MetadataResource {
   @Inject
   private MetadataManager metadataManager;
 
+  @GET
+  @Produces({"application/json"})
+  @ApiOperation(GET_METADATA_OD)
+  public LogsearchMetaData getMetadata(@Valid @BeanParam MetadataQueryRequest 
request) {
+    return metadataManager.getMetadata(request);
+  }
+
+  @GET
+  @Path("/list")
+  @Produces({"application/json"})
+  @ApiOperation(GET_METADATA_LIST_OD)
+  public Collection<LogsearchMetaData> getMetadataList(@Valid @BeanParam 
MetadataQueryRequest request) {
+    return metadataManager.getMetadataList(request);
+  }
+
   @POST
   @Produces({"application/json"})
   @ApiOperation(SAVE_METADATA_OD)
@@ -61,17 +79,25 @@ public class MetadataResource {
     return metadataManager.saveMetadata(metadata);
   }
 
+  @POST
+  @Path("/list")
+  @Produces({"application/json"})
+  @ApiOperation(SAVE_METADATA_LIST_OD)
+  public String saveMetadataList(Collection<LogsearchMetaData> metadata) {
+    return metadataManager.saveMetadata(metadata);
+  }
+
   @DELETE
   @ApiOperation(DELETE_METADATA_OD)
   public void deleteMetadata(LogsearchMetaData metadata) {
     metadataManager.deleteMetadata(metadata);
   }
 
-  @GET
-  @Produces({"application/json"})
-  @ApiOperation(GET_METADATA_OD)
-  public Collection<LogsearchMetaData> getMetadataList(@Valid @BeanParam 
MetadataQueryRequest request) {
-    return metadataManager.getMetadata(request);
+  @DELETE
+  @Path("/list")
+  @ApiOperation(DELETE_METADATA_LIST_OD)
+  public void deleteMetadataList(Collection<LogsearchMetaData> metadata) {
+    metadataManager.deleteMetadata(metadata);
   }
 
 }

Reply via email to