Search in sources :

Example 31 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class SQLMap method selectImpl.

/**
 * Fetches the components from the database matching the given cmp description, the fields that aren't
 * null within the cmp will match the where clause
 * @param not indicates if the query should be a "not" query
 * @param cmp the component to match
 * @param orderBy the column to order by, can be null to ignore order
 * @param ascending true to indicate ascending order
 * @param maxElements the maximum number of elements returned can be 0 or lower to ignore
 * @param page  the page within the query to match the max elements value
 * @return the result of the query
 */
private java.util.List<PropertyBusinessObject> selectImpl(boolean not, PropertyBusinessObject cmp, Property orderBy, boolean ascending, int maxElements, int page) throws IOException, InstantiationException {
    String tableName = getTableName(cmp);
    StringBuilder createStatement = new StringBuilder("SELECT * FROM ");
    createStatement.append(tableName);
    ArrayList<Object> params = new ArrayList<Object>();
    createStatement.append(" WHERE ");
    boolean found = false;
    for (PropertyBase p : cmp.getPropertyIndex()) {
        if (p instanceof Property) {
            if (((Property) p).get() != null) {
                if (found) {
                    createStatement.append(" AND ");
                }
                found = true;
                params.add(((Property) p).get());
                createStatement.append(getColumnName(p));
                if (not) {
                    createStatement.append(" <> ?");
                } else {
                    createStatement.append(" = ?");
                }
            }
        }
    }
    // all properties are null undo the where append
    if (!found) {
        createStatement = new StringBuilder("SELECT * FROM ");
        createStatement.append(tableName);
    }
    if (orderBy != null) {
        createStatement.append(" ORDER BY ");
        createStatement.append(getColumnName(orderBy));
        if (!ascending) {
            createStatement.append(" DESC");
        }
    }
    if (maxElements > 0) {
        createStatement.append(" LIMIT ");
        createStatement.append(maxElements);
        if (page > 0) {
            createStatement.append(" OFFSET ");
            createStatement.append(page * maxElements);
        }
    }
    Cursor c = null;
    try {
        ArrayList<PropertyBusinessObject> response = new ArrayList<PropertyBusinessObject>();
        c = executeQuery(createStatement.toString(), params.toArray());
        while (c.next()) {
            PropertyBusinessObject pb = (PropertyBusinessObject) cmp.getClass().newInstance();
            for (PropertyBase p : pb.getPropertyIndex()) {
                Row currentRow = c.getRow();
                SqlType t = getSqlType(p);
                if (t == SqlType.SQL_EXCLUDE) {
                    continue;
                }
                Object value = t.getValue(currentRow, c.getColumnIndex(getColumnName(p)), p);
                if (p instanceof Property) {
                    ((Property) p).set(value);
                }
            }
            response.add(pb);
        }
        c.close();
        return response;
    } catch (Throwable t) {
        Log.e(t);
        if (c != null) {
            c.close();
        }
        if (t instanceof IOException) {
            throw ((IOException) t);
        } else {
            throw new IOException(t.toString());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) Cursor(com.codename1.db.Cursor) Row(com.codename1.db.Row)

Example 32 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class CloudObject method bindTree.

/**
 * Binds a UI tree to the cloud object so its values automatically update in the cloud object
 *
 * @param ui the component tree to bind
 * @param defer bind settings whether to defer the binding which requires developers to explicitly commit
 * the binding to perform the changes
 * @param objectLead if set to true the UI property is initialized from values in the CloudObject, if false
 * the cloud object property is initialized from the UI
 */
public void bindTree(Container ui, int defer, boolean objectLead) {
    int componentCount = ui.getComponentCount();
    for (int iter = 0; iter < componentCount; iter++) {
        Component c = ui.getComponentAt(iter);
        if (c instanceof Container) {
            bindTree((Container) c, defer, objectLead);
            continue;
        }
        String bind = c.getCloudBoundProperty();
        if (bind != null && bind.length() > 0) {
            String attributeName = c.getCloudDestinationProperty();
            if (attributeName != null) {
                bindProperty(c, bind, attributeName, defer, objectLead);
            }
        }
    }
}
Also used : Container(com.codename1.ui.Container) Component(com.codename1.ui.Component)

Example 33 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class CloudObject method unbindProperty.

/**
 * Releases the binding for the specific property name
 * @param cmp the component
 * @param propertyName the name of the property
 */
public void unbindProperty(Component cmp, String propertyName) {
    BindTarget t = (BindTarget) cmp.getClientProperty("CN1Bind" + propertyName);
    cmp.unbindProperty(propertyName, t);
    ;
}
Also used : BindTarget(com.codename1.cloud.BindTarget)

Example 34 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class CloudObject method bindProperty.

/**
 * Binds a property value within the given component to this cloud object, this means that
 * when the component changes the cloud object changes unless deferred. If the defer flag is
 * false all changes are stored in a temporary location and only "committed" once commitBindings()
 * is invoked.
 * @param cmp the component to bind
 * @param propertyName the name of the property in the bound component
 * @param attributeName the key within the cloud object
 * @param defer bind settings whether to defer the binding which requires developers to explicitly commit
 * the binding to perform the changes
 * @param objectLead if set to true the UI property is initialized from values in the CloudObject, if false
 * the cloud object property is initialized from the UI
 */
public void bindProperty(Component cmp, final String propertyName, final String attributeName, final int defer, boolean objectLead) {
    if (objectLead) {
        Object val = values.get(attributeName);
        Object cmpVal = cmp.getBoundPropertyValue(propertyName);
        if (val == null) {
            if (cmpVal != null) {
                cmp.setBoundPropertyValue(propertyName, null);
            }
        } else {
            if (cmpVal == null || !(val.equals(cmpVal))) {
                cmp.setBoundPropertyValue(propertyName, val);
            }
        }
    } else {
        Object val = values.get(attributeName);
        Object cmpVal = cmp.getBoundPropertyValue(propertyName);
        if (cmpVal == null) {
            if (val != null) {
                values.remove(attributeName);
                status = STATUS_MODIFIED;
            }
        } else {
            if (val == null || !(val.equals(cmpVal))) {
                values.put(attributeName, cmpVal);
                status = STATUS_MODIFIED;
            }
        }
    }
    BindTarget target = new BindTarget() {

        public void propertyChanged(Component source, String propertyName, Object oldValue, Object newValue) {
            switch(defer) {
                case BINDING_DEFERRED:
                    if (deferedValues == null) {
                        deferedValues = new Hashtable();
                    }
                    deferedValues.put(attributeName, newValue);
                    break;
                case BINDING_IMMEDIATE:
                    values.put(attributeName, newValue);
                    status = STATUS_MODIFIED;
                    break;
                case BINDING_AUTO_SAVE:
                    values.put(attributeName, newValue);
                    status = STATUS_MODIFIED;
                    CloudStorage.getInstance().save(CloudObject.this);
                    break;
            }
        }
    };
    cmp.bindProperty(propertyName, target);
    cmp.putClientProperty("CN1Bind" + propertyName, target);
}
Also used : Hashtable(java.util.Hashtable) BindTarget(com.codename1.cloud.BindTarget) Component(com.codename1.ui.Component)

Example 35 with Property

use of com.codename1.rad.models.Property in project CodenameOne by codenameone.

the class PropertyIndex method loadJSONList.

/**
 * Loads JSON containing a list of property objects of this type
 * @param stream the input stream
 * @return list of property objects matching this type
 */
public <X extends PropertyBusinessObject> List<X> loadJSONList(InputStream stream) throws IOException {
    JSONParser jp = new JSONParser();
    JSONParser.setUseBoolean(true);
    JSONParser.setUseLongs(true);
    List<X> response = new ArrayList<X>();
    Map<String, Object> result = jp.parseJSON(new InputStreamReader(stream, "UTF-8"));
    List<Map> entries = (List<Map>) result.get("root");
    for (Map m : entries) {
        X pb = (X) newInstance();
        pb.getPropertyIndex().populateFromMap(m, parent.getClass());
        response.add(pb);
    }
    return response;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) JSONParser(com.codename1.io.JSONParser) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Entity (com.codename1.rad.models.Entity)22 Property (com.codename1.rad.models.Property)16 IOException (java.io.IOException)11 ResultParser (com.codename1.rad.io.ResultParser)10 ArrayList (java.util.ArrayList)10 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)9 Component (com.codename1.ui.Component)9 Element (com.codename1.xml.Element)8 ParseException (com.codename1.l10n.ParseException)7 Container (com.codename1.ui.Container)7 Label (com.codename1.ui.Label)7 SortedProperties (com.codename1.ant.SortedProperties)6 Log (com.codename1.io.Log)6 Form (com.codename1.ui.Form)6 AnimationObject (com.codename1.ui.animations.AnimationObject)6 Map (java.util.Map)6 Thing (com.codename1.rad.schemas.Thing)5 XMLParser (com.codename1.xml.XMLParser)5 StringReader (java.io.StringReader)5 List (java.util.List)5