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());
}
}
}
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);
}
}
}
}
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);
;
}
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);
}
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;
}
Aggregations