Search in sources :

Example 11 with AbstractBase

use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.

the class JSBase method checkModification.

@SuppressWarnings("unchecked")
public final void checkModification() {
    // first call parent, so the parent will make a copy.
    parent.checkModification();
    AbstractBase tempPersist = baseComponent;
    if (!isCopy) {
        // then get the replace the item with the item of the copied relation.
        baseComponent = (T) parent.getSupportChild().getChild(baseComponent.getUUID());
        isCopy = true;
    }
    baseComponent = (T) getOverridePersistIfNeeded(tempPersist, baseComponent, getJSParent());
}
Also used : AbstractBase(com.servoy.j2db.persistence.AbstractBase)

Example 12 with AbstractBase

use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.

the class JSBase method getOverridePersistIfNeeded.

public static AbstractBase getOverridePersistIfNeeded(AbstractBase persist, AbstractBase finalPersist, IJSParent parent) {
    AbstractBase baseComponent = finalPersist;
    if (persist != null && persist.getAncestor(IRepository.FORMS) != null) {
        IJSParent<?> jsparent = parent;
        while (jsparent != null) {
            if (jsparent.getSupportChild() instanceof Form)
                break;
            jsparent = jsparent.getJSParent();
        }
        if (jsparent != null && jsparent.getSupportChild() instanceof Form && !jsparent.getSupportChild().getUUID().equals(persist.getAncestor(IRepository.FORMS).getUUID())) {
            // inherited persist
            try {
                IPersist parentPersist = persist;
                while (PersistHelper.getSuperPersist((ISupportExtendsID) parentPersist) != null) {
                    parentPersist = PersistHelper.getSuperPersist((ISupportExtendsID) parentPersist);
                }
                // first check if the override persist already exists, and return it in that case
                List<IPersist> children = PersistHelper.getHierarchyChildren((AbstractBase) parent.getSupportChild());
                IPersist form = parent.getSupportChild() instanceof Form ? parent.getSupportChild() : parent.getSupportChild().getAncestor(IRepository.FORMS);
                if (form != null) {
                    for (IPersist p : children) {
                        if (parentPersist.getID() == ((ISupportExtendsID) p).getExtendsID() && form.equals(p.getAncestor(IRepository.FORMS))) {
                            return (AbstractBase) p;
                        }
                    }
                } else {
                    // should not happen
                    Debug.error("Cannot find form of persist " + parent.getSupportChild());
                }
                baseComponent = (AbstractBase) persist.cloneObj(parent.getSupportChild(), false, null, false, false, false);
                baseComponent.copyPropertiesMap(null, true);
                ((ISupportExtendsID) baseComponent).setExtendsID(parentPersist.getID());
            } catch (Exception ex) {
                Debug.error(ex);
            }
        }
    }
    return baseComponent;
}
Also used : Form(com.servoy.j2db.persistence.Form) IPersist(com.servoy.j2db.persistence.IPersist) ISupportExtendsID(com.servoy.j2db.persistence.ISupportExtendsID) AbstractBase(com.servoy.j2db.persistence.AbstractBase)

Example 13 with AbstractBase

use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.

the class PersistHelper method getHierarchyChildren.

public static List<IPersist> getHierarchyChildren(AbstractBase parent) {
    if (parent instanceof ISupportExtendsID) {
        List<IPersist> children = new ArrayList<IPersist>();
        List<AbstractBase> parentHierarchy = new ArrayList<AbstractBase>();
        List<Integer> existingIDs = new ArrayList<Integer>();
        AbstractBase element = parent;
        while (element != null && !parentHierarchy.contains(element)) {
            parentHierarchy.add(element);
            element = (AbstractBase) PersistHelper.getSuperPersist((ISupportExtendsID) element);
        }
        for (AbstractBase temp : parentHierarchy) {
            for (IPersist child : temp.getAllObjectsAsList()) {
                if (!(child instanceof ISupportExtendsID))
                    continue;
                Integer extendsID = new Integer(((ISupportExtendsID) child).getExtendsID());
                if (!existingIDs.contains(new Integer(child.getID())) && !existingIDs.contains(extendsID)) {
                    if (PersistHelper.isOverrideOrphanElement((ISupportExtendsID) child)) {
                        // some deleted element
                        continue;
                    }
                    existingIDs.add(child.getID());
                    children.add(child);
                }
                if (extendsID.intValue() > 0 && !existingIDs.contains(extendsID)) {
                    existingIDs.add(extendsID);
                }
            }
        }
        return children;
    } else {
        return parent.getAllObjectsAsList();
    }
}
Also used : ISupportExtendsID(com.servoy.j2db.persistence.ISupportExtendsID) IPersist(com.servoy.j2db.persistence.IPersist) ArrayList(java.util.ArrayList) AbstractBase(com.servoy.j2db.persistence.AbstractBase)

Example 14 with AbstractBase

use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.

the class PersistHelper method getFlattenedPropertiesMap.

/**
 * Similar to {@link AbstractBase#getPropertiesMap()}, but takes into account persist inheritance.
 * @param extendable a persist which could be inherit from another.
 * @return the map of property values collected from the persist's hierarchy chain.
 */
public static Map<String, Object> getFlattenedPropertiesMap(ISupportExtendsID extendable) {
    Map<String, Object> map = new HashMap<String, Object>();
    List<AbstractBase> hierarchy = PersistHelper.getOverrideHierarchy(extendable);
    for (int i = hierarchy.size() - 1; i >= 0; i--) {
        map.putAll(hierarchy.get(i).getPropertiesMap());
    }
    return map;
}
Also used : HashMap(java.util.HashMap) AbstractBase(com.servoy.j2db.persistence.AbstractBase) Point(java.awt.Point)

Example 15 with AbstractBase

use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.

the class SQLGenerator method createJoin.

/**
 * Join clause for this relation.
 */
public static ISQLTableJoin createJoin(IDataProviderHandler flattenedSolution, IRelation relation, BaseQueryTable primaryTable, BaseQueryTable foreignTable, boolean permanentJoin, final IGlobalValueEntry provider) throws RepositoryException {
    if (relation instanceof AbstractBase) {
        ISQLTableJoin queryJoin = ((AbstractBase) relation).getRuntimeProperty(Relation.RELATION_JOIN);
        if (queryJoin != null) {
            // a query join was defined for this relation, just relink the tables for the first and last in the joins
            queryJoin = deepClone(queryJoin);
            queryJoin = AbstractBaseQuery.relinkTable(queryJoin.getPrimaryTable(), primaryTable, queryJoin);
            queryJoin = AbstractBaseQuery.relinkTable(queryJoin.getForeignTable(), foreignTable, queryJoin);
            // update the placeholders for globals
            queryJoin.acceptVisitor(new IVisitor() {

                public Object visit(Object o) {
                    if (o instanceof Placeholder && ((Placeholder) o).getKey() instanceof ObjectPlaceholderKey) {
                        Object value = provider.getDataProviderValue(((ObjectPlaceholderKey<int[]>) ((Placeholder) o).getKey()).getName());
                        int[] args = ((ObjectPlaceholderKey<int[]>) ((Placeholder) o).getKey()).getObject();
                        int dataProviderType = args[0];
                        int flags = args[1];
                        if (value == null) {
                            return ValueFactory.createNullValue(dataProviderType);
                        }
                        return Column.getAsRightType(dataProviderType, flags, value, Integer.MAX_VALUE, false, false);
                    }
                    return o;
                }
            });
            return queryJoin;
        }
    }
    // build a join from the relation items
    IDataProvider[] primary = relation.getPrimaryDataProviders(flattenedSolution);
    Column[] foreign = relation.getForeignColumns(flattenedSolution);
    int[] operators = relation.getOperators();
    AndCondition joinCondition = new AndCondition();
    for (int x = 0; x < primary.length; x++) {
        Column primaryColumn = null;
        // check if stored script calc or table column
        if (primary[x] instanceof ScriptCalculation) {
            ScriptCalculation sc = ((ScriptCalculation) primary[x]);
            // null when not stored
            primaryColumn = sc.getTable().getColumn(sc.getName());
        } else if (primary[x] instanceof Column) {
            primaryColumn = (Column) primary[x];
        }
        QueryColumn foreignColumn = foreign[x].queryColumn(foreignTable);
        Object value;
        if (primaryColumn == null) {
            if (primary[x] instanceof LiteralDataprovider) {
                value = ((LiteralDataprovider) primary[x]).getValue();
                value = foreign[x].getAsRightType(value);
            } else {
                value = provider.getDataProviderValue(primary[x].getDataProviderID());
                if (value == null) {
                    value = ValueFactory.createNullValue(primary[x].getDataProviderType());
                } else if (value instanceof Placeholder) {
                    if (((Placeholder) value).getKey() instanceof ObjectPlaceholderKey<?>) {
                        ((ObjectPlaceholderKey) ((Placeholder) value).getKey()).setObject(new int[] { primary[x].getDataProviderType(), primary[x].getFlags() });
                    }
                } else {
                    value = Column.getAsRightType(primary[x].getDataProviderType(), primary[x].getFlags(), value, Integer.MAX_VALUE, false, false);
                }
            }
        } else // table type, can be stored calc
        {
            value = primaryColumn.queryColumn(primaryTable);
        }
        // all operators are swappable because only relation operators in RelationItem.RELATION_OPERATORS can be defined.
        // NOTE: elements in joinCondition MUST be CompareConditions (expected in QueryGenerator and SQLGenerator.createConditionFromFindState)
        joinCondition.addCondition(new CompareCondition(RelationItem.swapOperator(operators[x]), foreignColumn, value));
    }
    if (joinCondition.getConditions().size() == 0) {
        // $NON-NLS-1$
        throw new RepositoryException("Missing join condition in relation " + relation.getName());
    }
    return new QueryJoin(relation.getName(), primaryTable, foreignTable, joinCondition, relation.getJoinType(), permanentJoin);
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) IVisitor(com.servoy.j2db.util.visitor.IVisitor) AbstractBase(com.servoy.j2db.persistence.AbstractBase) QueryJoin(com.servoy.j2db.query.QueryJoin) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IDataProvider(com.servoy.j2db.persistence.IDataProvider) LiteralDataprovider(com.servoy.j2db.persistence.LiteralDataprovider) AndCondition(com.servoy.j2db.query.AndCondition) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) CompareCondition(com.servoy.j2db.query.CompareCondition) ObjectPlaceholderKey(com.servoy.j2db.query.ObjectPlaceholderKey)

Aggregations

AbstractBase (com.servoy.j2db.persistence.AbstractBase)26 IPersist (com.servoy.j2db.persistence.IPersist)12 Form (com.servoy.j2db.persistence.Form)9 JSONObject (org.json.JSONObject)8 IFormElement (com.servoy.j2db.persistence.IFormElement)6 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 ISupportExtendsID (com.servoy.j2db.persistence.ISupportExtendsID)5 Point (java.awt.Point)5 PropertyDescription (org.sablo.specification.PropertyDescription)5 RepositoryException (com.servoy.j2db.persistence.RepositoryException)4 IForm (com.servoy.j2db.IForm)3 Field (com.servoy.j2db.persistence.Field)3 FlattenedForm (com.servoy.j2db.persistence.FlattenedForm)3 IDataProvider (com.servoy.j2db.persistence.IDataProvider)3 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)3 FormController (com.servoy.j2db.FormController)2 IScriptExecuter (com.servoy.j2db.IScriptExecuter)2 BaseComponent (com.servoy.j2db.persistence.BaseComponent)2