Package org.geomajas.gwt.client.util

Source Code of org.geomajas.gwt.client.util.AttributeUtil

/*
* This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
*
* Copyright 2008-2011 Geosparc nv, http://www.geosparc.com/, Belgium.
*
* The program is available in open source according to the GNU Affero
* General Public License. All contributions in this program are covered
* by the Geomajas Contributors License Agreement. For full licensing
* details, see LICENSE.txt in the project root.
*/
package org.geomajas.gwt.client.util;

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

import org.geomajas.configuration.AssociationAttributeInfo;
import org.geomajas.configuration.AttributeInfo;
import org.geomajas.configuration.PrimitiveAttributeInfo;
import org.geomajas.layer.feature.Attribute;
import org.geomajas.layer.feature.attribute.AssociationAttribute;
import org.geomajas.layer.feature.attribute.AssociationValue;
import org.geomajas.layer.feature.attribute.BooleanAttribute;
import org.geomajas.layer.feature.attribute.CurrencyAttribute;
import org.geomajas.layer.feature.attribute.DateAttribute;
import org.geomajas.layer.feature.attribute.DoubleAttribute;
import org.geomajas.layer.feature.attribute.FloatAttribute;
import org.geomajas.layer.feature.attribute.ImageUrlAttribute;
import org.geomajas.layer.feature.attribute.IntegerAttribute;
import org.geomajas.layer.feature.attribute.LongAttribute;
import org.geomajas.layer.feature.attribute.ManyToOneAttribute;
import org.geomajas.layer.feature.attribute.OneToManyAttribute;
import org.geomajas.layer.feature.attribute.PrimitiveAttribute;
import org.geomajas.layer.feature.attribute.ShortAttribute;
import org.geomajas.layer.feature.attribute.StringAttribute;
import org.geomajas.layer.feature.attribute.UrlAttribute;

/**
* Utility for attribute manipulations.
*
* @author Jan De Moerloose
* @since 1.9.0
*
*/
public final class AttributeUtil {

  private AttributeUtil() {
  }

  /**
   * Creates an empty association value. An empty association value is a value for which the attributes and identifier
   * are empty.
   *
   * @param attributeInfo
   * @return an empty value
   */
  public static AssociationValue createEmptyAssociationValue(AssociationAttributeInfo attributeInfo) {
    AssociationValue value = new AssociationValue();
    Map<String, Attribute<?>> attributes = new HashMap<String, Attribute<?>>();
    for (AttributeInfo attrInfo : attributeInfo.getFeature().getAttributes()) {
      attributes.put(attrInfo.getName(), createEmptyAttribute(attrInfo));
    }
    value.setAllAttributes(attributes);
    value.setId(createEmptyPrimitiveAttribute(attributeInfo.getFeature().getIdentifier()));
    return value;
  }

  /**
   * Creates an empty attribute. An empty attribute is an attribute which has null as its value.
   *
   * @param attrInfo
   * @return an empty attribute.
   */
  public static Attribute<?> createEmptyAttribute(AttributeInfo attrInfo) {
    if (attrInfo instanceof PrimitiveAttributeInfo) {
      return createEmptyPrimitiveAttribute((PrimitiveAttributeInfo) attrInfo);
    } else if (attrInfo instanceof AssociationAttributeInfo) {
      return createEmptyAssociationAttribute((AssociationAttributeInfo) attrInfo);
    }
    return null;
  }

  /**
   * Creates an empty primitive attribute. An empty attribute is an attribute which has null as its value.
   *
   * @param info
   * @return an empty primitive attribute of the right type.
   */
  public static PrimitiveAttribute<?> createEmptyPrimitiveAttribute(PrimitiveAttributeInfo info) {
    PrimitiveAttribute<?> attribute = null;
    switch (info.getType()) {
      case BOOLEAN:
        attribute = new BooleanAttribute();
        break;
      case SHORT:
        attribute = new ShortAttribute();
        break;
      case INTEGER:
        attribute = new IntegerAttribute();
        break;
      case LONG:
        attribute = new LongAttribute();
        break;
      case FLOAT:
        attribute = new FloatAttribute();
        break;
      case DOUBLE:
        attribute = new DoubleAttribute();
        break;
      case CURRENCY:
        attribute = new CurrencyAttribute();
        break;
      case STRING:
        attribute = new StringAttribute();
        break;
      case DATE:
        attribute = new DateAttribute();
        break;
      case URL:
        attribute = new UrlAttribute();
        break;
      case IMGURL:
        attribute = new ImageUrlAttribute();
    }
    attribute.setEditable(info.isEditable());
    return attribute;
  }

  /**
   * Creates an empty association attribute. An empty attribute is an attribute which has null as its value.
   *
   * @param info
   * @return an empty association attribute of the right type.
   */
  public static AssociationAttribute<?> createEmptyAssociationAttribute(AssociationAttributeInfo info) {
    AssociationAttribute<?> association = null;
    switch (info.getType()) {
      case MANY_TO_ONE:
        association = new ManyToOneAttribute();
        break;
      case ONE_TO_MANY:
        association = new OneToManyAttribute();
    }
    association.setEditable(info.isEditable());
    return association;
  }
}
TOP

Related Classes of org.geomajas.gwt.client.util.AttributeUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.