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

com.linkedin.restli.examples.greetings.server.ComplexKeysDataProvider Maven / Gradle / Ivy

Go to download

Pegasus is a framework for building robust, scalable service architectures using dynamic discovery and simple asychronous type-checked REST + JSON APIs.

There is a newer version: 27.7.18
Show newest version
/*
   Copyright (c) 2012 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.examples.greetings.server;


import com.linkedin.data.transform.DataProcessingException;
import com.linkedin.restli.common.ComplexResourceKey;
import com.linkedin.restli.common.HttpStatus;
import com.linkedin.restli.common.PatchRequest;
import com.linkedin.restli.examples.StringTestKeys;
import com.linkedin.restli.examples.greetings.api.Message;
import com.linkedin.restli.examples.greetings.api.Tone;
import com.linkedin.restli.examples.greetings.api.TwoPartKey;
import com.linkedin.restli.server.BatchDeleteRequest;
import com.linkedin.restli.server.BatchPatchRequest;
import com.linkedin.restli.server.BatchResult;
import com.linkedin.restli.server.BatchUpdateRequest;
import com.linkedin.restli.server.BatchUpdateResult;
import com.linkedin.restli.server.RestLiServiceException;
import com.linkedin.restli.server.UpdateResponse;
import com.linkedin.restli.server.util.PatchApplier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class ComplexKeysDataProvider
{
  private Map _db = new HashMap();

  public ComplexKeysDataProvider()
  {
    addExample(StringTestKeys.URL,
               StringTestKeys.URL2,
               StringTestKeys.URL + " " + StringTestKeys.URL2);
    addExample(StringTestKeys.SIMPLEKEY,
               StringTestKeys.SIMPLEKEY2,
               StringTestKeys.SIMPLEKEY + " " + StringTestKeys.SIMPLEKEY2);
  }

  public Message get(ComplexResourceKey key)
  {
    return _db.get(keyToString(key.getKey()));
  }

  public ComplexResourceKey create(Message message)
  {
    TwoPartKey key = new TwoPartKey();
    key.setMajor(message.getMessage());
    key.setMinor(message.getMessage());

    _db.put(keyToString(key), message);
    return new ComplexResourceKey(key, new TwoPartKey());
  }

  public void partialUpdate(
      ComplexResourceKey key,
      PatchRequest patch) throws DataProcessingException
  {
    Message message = _db.get(keyToString(key.getKey()));
    PatchApplier.applyPatch(message, patch);
  }

  public List findByPrefix(String prefix)
  {
    ArrayList results = new ArrayList();

    for (Map.Entry entry : _db.entrySet())
    {
      if (entry.getKey().startsWith(prefix))
      {
        results.add(entry.getValue());
      }
    }

    return results;
  }

  public BatchResult, Message> batchGet(
      Set> keys)
  {
    Map, Message> data =
        new HashMap, Message>();
    Map, RestLiServiceException> errors =
        new HashMap, RestLiServiceException>();

    for (ComplexResourceKey key : keys)
    {
      String stringKey = keyToString(key.getKey());
      if (_db.containsKey(stringKey))
      {
        data.put(key, _db.get(stringKey));
      }
      else
      {
        errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
      }
    }

    return new BatchResult, Message>(data, errors);
  }

  public BatchUpdateResult, Message> batchUpdate(
      BatchUpdateRequest, Message> entities)
  {
    final Map, UpdateResponse> results =
        new HashMap, UpdateResponse>();
    final Map, RestLiServiceException> errors =
        new HashMap, RestLiServiceException>();

    for (Map.Entry, Message> entry : entities.getData().entrySet())
    {
      if (_db.containsKey(keyToString(entry.getKey().getKey())))
      {
        _db.put(keyToString(entry.getKey().getKey()), entry.getValue());
        results.put(entry.getKey(), new UpdateResponse(HttpStatus.S_200_OK));
      }
      else
      {
        errors.put(entry.getKey(), new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
      }
    }

    return new BatchUpdateResult, Message>(results, errors);
  }

  public BatchUpdateResult, Message> batchUpdate(
      BatchPatchRequest, Message> patches)
  {
    final Map, UpdateResponse> results =
        new HashMap, UpdateResponse>();

    for (Map.Entry, PatchRequest> patch : patches.getData().entrySet())
    {
      try
      {
        this.partialUpdate(patch.getKey(), patch.getValue());
        results.put(patch.getKey(), new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
      }
      catch (DataProcessingException e)
      {
        results.put(patch.getKey(), new UpdateResponse(HttpStatus.S_400_BAD_REQUEST));
      }
    }

    return new BatchUpdateResult, Message>(results);
  }

  public BatchUpdateResult, Message> batchDelete(
      BatchDeleteRequest, Message> ids)
  {
    final Map, UpdateResponse> results =
        new HashMap, UpdateResponse>();

    for (ComplexResourceKey id : ids.getKeys())
    {
      _db.remove(keyToString(id.getKey()));
      results.put(id, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    }

    return new BatchUpdateResult, Message>(results);
  }

  public List getAll()
  {
    ArrayList results = new ArrayList();

    for (Map.Entry entry : _db.entrySet())
    {
      results.add(entry.getValue());
    }

    return results;
  }

  private String keyToString(TwoPartKey key)
  {
    return key.getMajor() + " " + key.getMinor();
  }

  private void addExample(String majorKey, String minorKey, String messageText)
  {
    TwoPartKey key = new TwoPartKey();
    key.setMajor(majorKey);
    key.setMinor(minorKey);
    Message message = new Message();
    message.setId(keyToString(key));
    message.setMessage(messageText);
    message.setTone(Tone.SINCERE);
    _db.put(keyToString(key), message);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy