001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *                               ***
008 *
009 *   Community License: GPL 3.0
010 *
011 *   This file is free software: you can redistribute it and/or modify
012 *   it under the terms of the GNU General Public License as published
013 *   by the Free Software Foundation, either version 3 of the License,
014 *   or (at your option) any later version.
015 *
016 *   This file is distributed in the hope that it will be useful, but
017 *   WITHOUT ANY WARRANTY; without even the implied warranty of
018 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019 *   GNU General Public License for more details.
020 *
021 *   You should have received a copy of the GNU General Public License
022 *   along with this program. If not, see <http://www.gnu.org/licenses/>.
023 *
024 *                               ***
025 *
026 *   Available Commercial License: GraniteDS SLA 1.0
027 *
028 *   This is the appropriate option if you are creating proprietary
029 *   applications and you are not prepared to distribute and share the
030 *   source code of your application under the GPL v3 license.
031 *
032 *   Please visit http://www.granitedataservices.com/license for more
033 *   details.
034 */
035package org.granite.client.tide.data.impl;
036
037import java.util.Date;
038
039import org.granite.client.tide.PropertyHolder;
040import org.granite.client.tide.data.spi.DataManager;
041
042/**
043 * @author William DRAI
044 */
045public class ObjectUtil {
046
047    public static boolean isSimple(Object value) {
048        return value instanceof String || value instanceof Boolean || value instanceof Number || value instanceof Date;
049    }
050    
051    public static String toString(Object obj) {
052        return obj != null ? obj.toString() : "null";
053    }
054    
055    /**
056     *  Equality for objects, using uid property when possible
057     *
058     *  @param obj1 object
059     *  @param obj2 object
060     * 
061     *  @return true when objects are instances of the same entity
062     */ 
063    public static boolean objectEquals(DataManager dataManager, Object obj1, Object obj2) {
064        if ((obj1 instanceof PropertyHolder && dataManager.isEntity(obj2)) || (dataManager.isEntity(obj1) && obj2 instanceof PropertyHolder))
065            return false;
066        
067        if (dataManager.isEntity(obj1) && dataManager.isEntity(obj2) && obj1.getClass() == obj2.getClass()) {
068            if (!dataManager.isInitialized(obj1) || !dataManager.isInitialized(obj2)) {
069                // Compare with identifier for uninitialized entities
070                try {
071                    return objectEquals(dataManager, dataManager.getId(obj1), dataManager.getId(obj2));
072                }
073                catch (Exception e) {
074                        // No @Id;
075                        return obj1.equals(obj2);
076                }
077            }
078            return dataManager.getUid(obj1).equals(dataManager.getUid(obj2));
079        }
080        
081        if (obj1 == null)
082                return obj2 == null;
083        
084        return obj1.equals(obj2);
085    }
086}