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

eu.agrosense.shared.model.AgroEntity Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2008-2013 LimeTri. All rights reserved.
 *
 * AgroSense is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it
 * is applied to this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * AgroSense. If not, see .
 */
package eu.agrosense.shared.model;

import eu.limetri.api.model.aspect.HasParentId;
import java.util.Map;
import org.opendolphin.core.PresentationModel;

/**
 * Describing common functionality of Entity's For implementation extend
 * {@link AbstractEntity}
 *
 * @author johan
 */
public interface AgroEntity extends BaseEntity, HasParentId {

    public static final String PROP_FARM_URI = "farm_URI";
    AgroURI getFarmURI();
    
    // keep for backward compatibility for now:
    public static final String PROP_URI = PROP_ID;
    public static final String PROP_PARENT_URI = PROP_PARENT_ID;
    public static final String PROP_ITEM_ID_TYPE = PROP_ENTITY_TYPE;
    
    public static class check extends BaseEntity.check {

        /**
         * Checks provided value map for required attributes.
         * 
         * @param valueMap 
         * @throws IllegalArgumentException when one or more attributes are
         * missing
         */
        public static void requiredAttributes(Map valueMap){
            BaseEntity.check.requiredAttributes(valueMap);

            if (!valueMap.containsKey(AgroEntity.PROP_FARM_URI) || valueMap.get(AgroEntity.PROP_FARM_URI) == null) {
                throw new IllegalArgumentException("Provided value map did not contain a value for the required attribute PROP_FARM_URI");
            }            
        }
        
        /**
         * Checks provided presentation model for required attributes.
         *
         * @param model
         * @throws IllegalArgumentException when one or more attributes are
         * missing
         */
        public static void requiredAttributes(PresentationModel model) {
            BaseEntity.check.requiredAttributes(model);

            if (model.getAt(AgroEntity.PROP_FARM_URI) == null || model.getAt(AgroEntity.PROP_FARM_URI).getValue() == null) {
                throw new IllegalArgumentException("Provided presentation model did not contain a value for the required attribute PROP_FARM_URI");
            }
        }
    }
    
    public static abstract class BeanImpl implements AgroEntity {
        protected final AgroURI uri;
        protected final AgroURI farmURI;
        protected AgroURI parentURI;

        public BeanImpl(AgroURI uri, AgroURI farmURI) {
            this.uri = uri;
            this.farmURI = farmURI;
        }

        @Override
        public AgroURI getId() {
            return uri;
        }

        @Override
        public AgroURI getFarmURI() {
            return farmURI;
        }

        @Override
        public AgroURI getParentId() {
            return parentURI;
        }

        public void setParentURI(AgroURI parentURI) {
            this.parentURI = parentURI;
        }
        
        @Override
        public ItemIdType getEntityType() {
            return uri.getEntityType();
        }
        
        @Override
        public String getItemIdNumber() {
            return uri.getItemIdNumber();
        }
        
        //TODO: consider moving `asPresentationModel()` to a separate interface, or treat it as an optional method
        //(as sometimes we may want to create a BeanImpl from a data response and skip using a model altogether).
        @Override
        public PresentationModel asPresentationModel() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    
    
    public static abstract class ModelImpl implements AgroEntity {
        protected final PresentationModel model;

        public ModelImpl(PresentationModel model) {
            this.model = model;
        }
        
        @Override
        public AgroURI getId() {
            return PM.getURI(model);
        }

        @Override
        public AgroURI getParentId() {
            return PM.getParentURI(model);
        }

        @Override
        public AgroURI getFarmURI() {
            return PM.getFarmURI(model);
        }

        @Override
        public ItemIdType getEntityType() {
            return PM.getType(model);
        }
        
        @Override
        public String getItemIdNumber() {
            return PM.getStringAttr(model, PROP_ITEM_ID_NUMBER);
        }

        @Override
        public PresentationModel asPresentationModel() {
            return model;
        }
        
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy