All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linkedin.restli.common.BatchCreateIdEntityResponse Maven / Gradle / Ivy

/*
   Copyright (c) 2015 LinkedIn Corp.

   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.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.linkedin.restli.common;

import com.linkedin.data.DataList;
import com.linkedin.data.DataMap;
import com.linkedin.data.collections.CheckedUtil;
import com.linkedin.data.schema.ArrayDataSchema;
import com.linkedin.data.schema.Name;
import com.linkedin.data.schema.RecordDataSchema;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.restli.internal.common.CreateIdEntityStatusDecoder;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Response for Batch Create id entity Requests that contain strongly typed keys and values.
 *
 * @author Boyang Chen
 */
public class BatchCreateIdEntityResponse extends RecordTemplate
{
  private final List> _collection;

  public BatchCreateIdEntityResponse(DataMap map, CreateIdEntityStatusDecoder idEntityDecoder)
      throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException
  {
    super(map, generateSchema());
    _collection = createCollectionFromDecoder(idEntityDecoder);
  }

  public BatchCreateIdEntityResponse(List> elements)
  {
    super(generateDataMap(elements), generateSchema());
    _collection = elements;

  }

  private static DataMap generateDataMap(List elements)
  {
    DataMap dataMap = new DataMap();
    DataList listElements = new DataList();
    for (RecordTemplate recordTemplate : elements)
    {
      CreateIdEntityStatus status = (CreateIdEntityStatus) recordTemplate;
      CheckedUtil.addWithoutChecking(listElements, status.data());
    }
    dataMap.put(CollectionResponse.ELEMENTS, listElements);

    return dataMap;
  }

  private static RecordDataSchema generateSchema()
  {
    StringBuilder errorMessageBuilder = new StringBuilder(10);
    ArrayDataSchema arraySchema = new ArrayDataSchema(new RecordDataSchema(new Name(CreateStatus.class.getSimpleName()), RecordDataSchema.RecordType.RECORD));
    RecordDataSchema.Field arrayField = new RecordDataSchema.Field(arraySchema);
    arrayField.setName(CollectionResponse.ELEMENTS, errorMessageBuilder);
    RecordDataSchema schema = new RecordDataSchema(new Name(BatchCreateIdEntityResponse.class.getSimpleName()), RecordDataSchema.RecordType.RECORD);
    schema.setFields(Arrays.asList(arrayField), errorMessageBuilder);
    return schema;
  }

  private List> createCollectionFromDecoder(CreateIdEntityStatusDecoder decoder)
      throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException
  {
    DataList elements = this.data().getDataList(CollectionResponse.ELEMENTS);
    List> collection = new ArrayList>(elements.size());
    for (Object obj : elements)
    {
      DataMap dataMap = (DataMap) obj;
      CreateIdEntityStatus status = decodeValue(dataMap, decoder);
      collection.add(status);
    }
    return collection;
  }

  private CreateIdEntityStatus decodeValue(DataMap dataMap, CreateIdEntityStatusDecoder decoder)
      throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException
  {
    return decoder.makeValue(dataMap);
  }

  public List> getElements()
  {
    return _collection;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy