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

com.backendless.commons.persistence.EntityDescription Maven / Gradle / Ivy

/*
 **********************************************************************************************************************
 *
 * BACKENDLESS.COM CONFIDENTIAL
 *
 **********************************************************************************************************************
 *
 *  Copyright 2012 BACKENDLESS.COM. All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains the property of Backendless.com and its suppliers,
 * if any.  The intellectual and technical concepts contained herein are proprietary to Backendless.com and its
 * suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret
 * or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden
 * unless prior written permission is obtained from Backendless.com.
 *
 **********************************************************************************************************************/
package com.backendless.commons.persistence;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.util.HashMap;
import java.util.Map;

@NoArgsConstructor
@JsonIgnoreProperties( { "field", "fieldsKeys", "name", "id" } )
@EqualsAndHashCode( of = { "fields" } )
@ToString
public class EntityDescription implements DataItem
{
  @Getter @Setter
  protected String name;
  @Getter
  protected Map fields = new HashMap<>();

  public EntityDescription( String name )
  {
    this.name = name;
  }

  public EntityDescription( String entityName, Map entity )
  {
    this.name = entityName;
    this.fields.putAll( entity );
  }

  public void addField( String name, Object value )
  {
    fields.put( name, value );
  }

  public Object getField( String key )
  {
    return fields.get( key );
  }

  public  T getField( String key, Class type )
  {
    return type.cast( fields.get( key ) );
  }

  public Object getId( String objectId )
  {
    String id = (String) getField( objectId );

    int delimiterIndex = id.indexOf( "." );
    if( delimiterIndex != -1 )
      id = id.substring( 0, delimiterIndex );

    return id;
  }

  public void setFields( Map fields )
  {
    this.fields.clear();
    this.fields.putAll( fields );
  }

  /**
   * Appends namespace to this entity's name.
   *
   * @param namespace namespace to be appended; if null or empty, no changes occur
   * @return the same object with namespace appended to its name
   */
  public EntityDescription withNamespace( String namespace )
  {
    if( namespace != null && !namespace.isEmpty() )
      setName( namespace + "." + name );
    return this;
  }

  public boolean containsField( String fieldName )
  {
    return fields.containsKey( fieldName );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy