Example usage for java.io Serializable equals

List of usage examples for java.io Serializable equals

Introduction

In this page you can find the example usage for java.io Serializable equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:org.nuxeo.ecm.core.storage.sql.HierarchyContext.java

/**
 * Checks that we don't move/copy under ourselves.
 *///from   w  w w. ja v  a2  s  .  c o  m
protected void checkNotUnder(Serializable parentId, Serializable id, String op) throws StorageException {
    Serializable pid = parentId;
    do {
        if (pid.equals(id)) {
            throw new StorageException(
                    "Cannot " + op + " a node under itself: " + parentId + " is under " + id);
        }
        SimpleFragment p = (SimpleFragment) get(pid, false);
        if (p == null) {
            // cannot happen
            throw new StorageException("No parent: " + pid);
        }
        pid = p.get(model.HIER_PARENT_KEY);
    } while (pid != null);
}

From source file:org.nuxeo.ecm.core.storage.sql.HierarchyContext.java

/**
 * Move a child to a new parent with a new name.
 *
 * @param source the source/*from  ww w  .  j  ava  2  s .  co  m*/
 * @param parentId the destination parent id
 * @param name the new name
 * @throws StorageException
 */
public void moveChild(Node source, Serializable parentId, String name) throws StorageException {
    // a save() has already been done by the caller
    Serializable id = source.getId();
    SimpleFragment hierFragment = source.getHierFragment();
    Serializable oldParentId = hierFragment.get(model.HIER_PARENT_KEY);
    String oldName = hierFragment.getString(model.HIER_CHILD_NAME_KEY);
    if (!oldParentId.equals(parentId)) {
        checkNotUnder(parentId, id, "move");
    } else if (oldName.equals(name)) {
        // null move
        return;
    }
    boolean complexProp = complexProp(hierFragment);
    checkFreeName(hierFragment, parentId, name, complexProp);
    /*
     * Do the move.
     */
    if (!oldName.equals(name)) {
        hierFragment.put(model.HIER_CHILD_NAME_KEY, name);
    }
    removeChild(hierFragment, complexProp);
    hierFragment.put(model.HIER_PARENT_KEY, parentId);
    addExistingChild(hierFragment, complexProp);
}

From source file:org.nuxeo.ecm.core.storage.sql.HierarchyContext.java

/**
 * Copy a child to a new parent with a new name.
 *
 * @param source the source of the copy//from   w ww. ja v  a 2s .c  om
 * @param parentId the destination parent id
 * @param name the new name
 * @return the id of the copy
 * @throws StorageException
 */
public Serializable copyChild(Node source, Serializable parentId, String name) throws StorageException {
    Serializable id = source.getId();
    SimpleFragment hierFragment = source.getHierFragment();
    Serializable oldParentId = hierFragment.get(model.HIER_PARENT_KEY);
    if (!oldParentId.equals(parentId)) {
        checkNotUnder(parentId, id, "copy");
    }
    checkFreeName(hierFragment, parentId, name, complexProp(hierFragment));
    /*
     * Do the copy.
     */
    String typeName = source.getPrimaryType();
    Serializable newId = mapper.copyHierarchy(id, typeName, parentId, name, null, null, persistenceContext);
    get(newId, false); // adds it as a new child of its parent
    return newId;
}

From source file:org.nuxeo.ecm.core.storage.sql.HierarchyContext.java

/**
 * Saves the created main rows, and returns the map of temporary ids to
 * final ids./*from w ww  .j  av  a  2s.c  o m*/
 * <p>
 * The parent ids of created children have to be mapped on the fly from
 * previously generated parent ids. This means that parents have to be
 * created before children, which is the case because "modified" is a linked
 * hashmap.
 *
 * @param createdIds the created ids to save
 * @return the map of created ids to final ids (when different)
 * @throws StorageException
 */
public Map<Serializable, Serializable> saveCreated(Set<Serializable> createdIds) throws StorageException {
    Map<Serializable, Serializable> idMap = null;
    for (Serializable id : createdIds) {
        SimpleFragment row = (SimpleFragment) modified.remove(id);
        if (row == null) {
            // was created and deleted before save
            continue;
        }
        if (idMap != null) {
            remapFragmentOnSave(row, idMap);
        }
        Serializable newId = mapper.insertSingleRow(row);
        row.setPristine();
        pristine.put(id, row);
        // save in translation map, if different
        // only happens for DB_IDENTITY id generation policy
        if (!newId.equals(id)) {
            if (idMap == null) {
                idMap = new HashMap<Serializable, Serializable>();
            }
            idMap.put(id, newId);
        }
    }
    return idMap == null ? Collections.<Serializable, Serializable>emptyMap() : idMap;
}

From source file:org.nuxeo.ecm.core.storage.sql.jdbc.JDBCRowMapper.java

/**
 * Reads several collection rows, given a table name and the ids.
 *
 * @param tableName the table name/*w  w  w  .j ava 2 s.  c  om*/
 * @param ids the ids
 */
protected List<Row> readCollectionArrays(String tableName, Collection<Serializable> ids)
        throws StorageException {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }
    String[] orderBys = { model.MAIN_KEY, model.COLL_TABLE_POS_KEY }; // clusters
                                                                      // results
    Set<String> skipColumns = new HashSet<String>(Arrays.asList(model.COLL_TABLE_POS_KEY));
    SQLInfoSelect select = sqlInfo.getSelectFragmentsByIds(tableName, ids.size(), orderBys, skipColumns);

    String sql = select.sql;
    try {
        if (logger.isLogEnabled()) {
            logger.logSQL(sql, ids);
        }
        PreparedStatement ps = connection.prepareStatement(sql);
        try {
            int i = 1;
            for (Serializable id : ids) {
                dialect.setId(ps, i++, id);
            }
            ResultSet rs = ps.executeQuery();
            countExecute();

            // get all values from result set, separate by ids
            // the result set is ordered by id, pos
            CollectionIO io = getCollectionIO(tableName);
            PropertyType ftype = model.getCollectionFragmentType(tableName);
            PropertyType type = ftype.getArrayBaseType();
            Serializable curId = null;
            List<Serializable> list = null;
            Serializable[] returnId = new Serializable[1];
            int[] returnPos = { -1 };
            List<Row> res = new LinkedList<Row>();
            Set<Serializable> remainingIds = new HashSet<Serializable>(ids);
            while (rs.next()) {
                Serializable value = io.getCurrentFromResultSet(rs, select.whatColumns, model, returnId,
                        returnPos);
                Serializable newId = returnId[0];
                if (newId != null && !newId.equals(curId)) {
                    // flush old list
                    if (list != null) {
                        res.add(new Row(tableName, curId, type.collectionToArray(list)));
                        remainingIds.remove(curId);
                    }
                    curId = newId;
                    list = new ArrayList<Serializable>();
                }
                list.add(value);
            }
            if (curId != null && list != null) {
                // flush last list
                res.add(new Row(tableName, curId, type.collectionToArray(list)));
                remainingIds.remove(curId);
            }

            // fill empty ones
            if (!remainingIds.isEmpty()) {
                Serializable[] emptyArray = ftype.getEmptyArray();
                for (Serializable id : remainingIds) {
                    res.add(new Row(tableName, id, emptyArray));
                }
            }
            if (logger.isLogEnabled()) {
                for (Row row : res) {
                    logger.log("  -> " + row);
                }
            }
            return res;
        } finally {
            closeStatement(ps);
        }
    } catch (Exception e) {
        checkConnectionReset(e);
        throw new StorageException("Could not select: " + sql, e);
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.jdbc.JDBCRowMapper.java

/**
 * Copy the rows from tableName with given ids into new ones with new ids
 * given by idMap./*from   ww  w. ja  va2s .com*/
 * <p>
 * A new row with id {@code overwriteId} is first deleted.
 *
 * @return {@link Boolean#TRUE} for a modification or creation,
 *         {@link Boolean#FALSE} for a deletion, {@code null} otherwise
 *         (still absent)
 * @throws SQLException
 */
protected Boolean copyRows(String tableName, Set<Serializable> ids, Map<Serializable, Serializable> idMap,
        Serializable overwriteId) throws SQLException {
    String copySql = sqlInfo.getCopySql(tableName);
    Column copyIdColumn = sqlInfo.getCopyIdColumn(tableName);
    PreparedStatement copyPs = connection.prepareStatement(copySql);
    String deleteSql = sqlInfo.getDeleteSql(tableName);
    PreparedStatement deletePs = connection.prepareStatement(deleteSql);
    try {
        boolean before = false;
        boolean after = false;
        for (Serializable id : ids) {
            Serializable newId = idMap.get(id);
            boolean overwrite = newId.equals(overwriteId);
            if (overwrite) {
                // remove existing first
                if (logger.isLogEnabled()) {
                    logger.logSQL(deleteSql, Collections.singletonList(newId));
                }
                dialect.setId(deletePs, 1, newId);
                int delCount = deletePs.executeUpdate();
                countExecute();
                logger.logCount(delCount);
                before = delCount > 0;
            }
            copyIdColumn.setToPreparedStatement(copyPs, 1, newId);
            copyIdColumn.setToPreparedStatement(copyPs, 2, id);
            if (logger.isLogEnabled()) {
                logger.logSQL(copySql, Arrays.asList(newId, id));
            }
            int copyCount = copyPs.executeUpdate();
            countExecute();
            logger.logCount(copyCount);
            if (overwrite) {
                after = copyCount > 0;
            }
        }
        // * , n -> mod (TRUE)
        // n , 0 -> del (FALSE)
        // 0 , 0 -> null
        return after ? Boolean.TRUE : (before ? Boolean.FALSE : null);
    } finally {
        closeStatement(copyPs);
        closeStatement(deletePs);
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.Mapper.java

/**
 * Copy the rows from tableName with ids in fragmentIds into new ones with
 * new ids given by idMap./*from   w  w  w.j  a va2s  .co m*/
 *
 * @return {@link Boolean#TRUE} for a modification or creation,
 *         {@link Boolean#FALSE} for a deletion, {@code null} otherwise
 *         (still absent)
 * @throws SQLException
 */
protected Boolean copyFragments(String tableName, Set<Serializable> ids, Map<Serializable, Serializable> idMap,
        Serializable overwriteId) throws SQLException {
    String copySql = sqlInfo.getCopySql(tableName);
    Column copyIdColumn = sqlInfo.getCopyIdColumn(tableName);
    PreparedStatement copyPs = connection.prepareStatement(copySql);
    String deleteSql = sqlInfo.getDeleteSql(tableName);
    PreparedStatement deletePs = connection.prepareStatement(deleteSql);
    try {
        boolean before = false;
        boolean after = false;
        for (Serializable id : ids) {
            Serializable newId = idMap.get(id);
            boolean overwrite = newId.equals(overwriteId);
            if (overwrite) {
                // remove existing first
                if (isLogEnabled()) {
                    logSQL(deleteSql, Collections.singletonList(newId));
                }
                deletePs.setObject(1, newId);
                int delCount = deletePs.executeUpdate();
                logCount(delCount);
                before = delCount > 0;
            }
            copyIdColumn.setToPreparedStatement(copyPs, 1, newId);
            copyIdColumn.setToPreparedStatement(copyPs, 2, id);
            if (isLogEnabled()) {
                logSQL(copySql, Arrays.asList(newId, id));
            }
            int copyCount = copyPs.executeUpdate();
            logCount(copyCount);
            if (overwrite) {
                after = copyCount > 0;
            }
        }
        // * , n -> mod (TRUE)
        // n , 0 -> del (FALSE)
        // 0 , 0 -> null
        return after ? Boolean.TRUE : (before ? Boolean.FALSE : null);
    } finally {
        closePreparedStatement(copyPs);
        closePreparedStatement(deletePs);
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.PersistenceContext.java

/**
 * Order a child before another./*from   w  w  w.  ja  v a2  s  .  c  om*/
 *
 * @param parentId the parent id
 * @param sourceId the node id to move
 * @param destId the node id before which to place the source node, if
 *            {@code null} then move the source to the end
 */
public void orderBefore(Serializable parentId, Serializable sourceId, Serializable destId)
        throws StorageException {
    boolean complexProp = false;
    if (!isOrderable(parentId, complexProp)) {
        // TODO throw exception?
        return;
    }
    if (sourceId.equals(destId)) {
        return;
    }
    // This is optimized by assuming the number of children is small enough
    // to be manageable in-memory.
    // fetch children and relevant nodes
    List<SimpleFragment> fragments = getChildren(parentId, null, complexProp);
    // renumber fragments
    int i = 0;
    SimpleFragment source = null; // source if seen
    Long destPos = null;
    for (SimpleFragment fragment : fragments) {
        Serializable id = fragment.getId();
        if (id.equals(destId)) {
            destPos = Long.valueOf(i);
            i++;
            if (source != null) {
                source.put(Model.HIER_CHILD_POS_KEY, destPos);
            }
        }
        Long setPos;
        if (id.equals(sourceId)) {
            i--;
            source = fragment;
            setPos = destPos;
        } else {
            setPos = Long.valueOf(i);
        }
        if (setPos != null) {
            if (!setPos.equals(fragment.get(Model.HIER_CHILD_POS_KEY))) {
                fragment.put(Model.HIER_CHILD_POS_KEY, setPos);
            }
        }
        i++;
    }
    if (destId == null) {
        Long setPos = Long.valueOf(i);
        if (!setPos.equals(source.get(Model.HIER_CHILD_POS_KEY))) {
            source.put(Model.HIER_CHILD_POS_KEY, setPos);
        }
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.PersistenceContext.java

/** Checks that we don't move/copy under ourselves. */
protected void checkNotUnder(Serializable parentId, Serializable id, String op) throws StorageException {
    Serializable pid = parentId;
    do {//from   w w  w. j a  va 2  s  . c o  m
        if (pid.equals(id)) {
            throw new StorageException(
                    "Cannot " + op + " a node under itself: " + parentId + " is under " + id);
        }
        SimpleFragment p = getHier(pid, false);
        if (p == null) {
            // cannot happen
            throw new StorageException("No parent: " + pid);
        }
        pid = p.get(Model.HIER_PARENT_KEY);
    } while (pid != null);
}

From source file:org.nuxeo.ecm.core.storage.sql.PersistenceContext.java

/**
 * Move a child to a new parent with a new name.
 *
 * @param source the source/*from ww w . j a  v  a  2 s. c  o m*/
 * @param parentId the destination parent id
 * @param name the new name
 * @throws StorageException
 */
public void move(Node source, Serializable parentId, String name) throws StorageException {
    // a save() has already been done by the caller when doing
    // an actual move (different parents)
    Serializable id = source.getId();
    SimpleFragment hierFragment = source.getHierFragment();
    Serializable oldParentId = hierFragment.get(Model.HIER_PARENT_KEY);
    String oldName = hierFragment.getString(Model.HIER_CHILD_NAME_KEY);
    if (!oldParentId.equals(parentId)) {
        checkNotUnder(parentId, id, "move");
    } else if (oldName.equals(name)) {
        // null move
        return;
    }
    boolean complexProp = complexProp(hierFragment);
    checkFreeName(parentId, name, complexProp);
    /*
     * Do the move.
     */
    if (!oldName.equals(name)) {
        hierFragment.put(Model.HIER_CHILD_NAME_KEY, name);
    }
    // cache management
    getHierSelectionContext(complexProp).recordRemoved(hierFragment);
    hierFragment.put(Model.HIER_PARENT_KEY, parentId);
    getHierSelectionContext(complexProp).recordExisting(hierFragment, true);
    // path invalidated
    source.path = null;
}