use of com.codename1.properties.PropertyBusinessObject in project CodenameOne by codenameone.
the class SQLMap method createTable.
/**
* Creates a table matching the given property component if one doesn't exist yet
* @param cmp the business object
* @return true if the table was created false if it already existed
*/
public boolean createTable(PropertyBusinessObject cmp) throws IOException {
String tableName = getTableName(cmp);
Cursor cr = null;
boolean has = false;
try {
cr = executeQuery("SELECT * FROM sqlite_master WHERE type='table' AND name='" + tableName + "'");
has = cr.next();
} finally {
if (cr != null) {
cr.close();
}
}
if (has) {
return false;
}
StringBuilder createStatement = new StringBuilder("CREATE TABLE ");
createStatement.append(tableName);
createStatement.append(" (");
String pkName = (String) cmp.getPropertyIndex().getMetaDataOfClass("cn1$pk");
boolean autoIncrement = cmp.getPropertyIndex().getMetaDataOfClass("cn1$autoinc") != null;
boolean first = true;
for (PropertyBase p : cmp.getPropertyIndex()) {
SqlType tp = getSqlType(p);
if (tp == SqlType.SQL_EXCLUDE) {
continue;
}
if (!first) {
createStatement.append(",");
}
first = false;
String columnName = getColumnName(p);
createStatement.append(columnName);
createStatement.append(" ");
createStatement.append(tp.dbType);
if (columnName.equalsIgnoreCase(pkName)) {
createStatement.append(" PRIMARY KEY");
if (autoIncrement) {
createStatement.append(" AUTOINCREMENT");
}
}
}
createStatement.append(")");
execute(createStatement.toString());
return true;
}
use of com.codename1.properties.PropertyBusinessObject in project CodenameOne by codenameone.
the class InstantUI method createEditUI.
/**
* Creates editing UI for the given business object
* @param bo the business object
* @param autoCommit true if the bindings used should be auto-committed
* @return a UI container that can be used to edit the business object
*/
public Container createEditUI(PropertyBusinessObject bo, boolean autoCommit) {
Container cnt;
if (Display.getInstance().isTablet()) {
TableLayout tl = new TableLayout(1, 2);
tl.setGrowHorizontally(true);
cnt = new Container(tl);
} else {
cnt = new Container(BoxLayout.y());
}
UiBinding uib = new UiBinding();
ArrayList<UiBinding.Binding> allBindings = new ArrayList<UiBinding.Binding>();
for (PropertyBase b : bo.getPropertyIndex()) {
if (isExcludedProperty(b)) {
continue;
}
Class cls = (Class) b.getClientProperty("cn1$cmpCls");
if (cls != null) {
try {
Component cmp = (Component) cls.newInstance();
cmp.setName(b.getName());
cnt.add(b.getLabel()).add(cmp);
allBindings.add(uib.bind(b, cmp));
} catch (Exception err) {
Log.e(err);
throw new RuntimeException("Custom property instant UI failed for " + b.getName() + " " + err);
}
continue;
}
String[] multiLabels = (String[]) b.getClientProperty("cn1$multiChceLbl");
if (multiLabels != null) {
// multi choice component
final Object[] multiValues = (Object[]) b.getClientProperty("cn1$multiChceVal");
if (multiLabels.length < 5) {
// toggle buttons
ButtonGroup bg = new ButtonGroup();
RadioButton[] rbs = new RadioButton[multiLabels.length];
cnt.add(b.getLabel());
Container radioBox = new Container(new GridLayout(multiLabels.length));
for (int iter = 0; iter < multiLabels.length; iter++) {
rbs[iter] = RadioButton.createToggle(multiLabels[iter], bg);
radioBox.add(rbs[iter]);
}
cnt.add(radioBox);
allBindings.add(uib.bindGroup(b, multiValues, rbs));
} else {
Picker stringPicker = new Picker();
stringPicker.setStrings(multiLabels);
Map<Object, Object> m1 = new HashMap<Object, Object>();
Map<Object, Object> m2 = new HashMap<Object, Object>();
for (int iter = 0; iter < multiLabels.length; iter++) {
m1.put(multiLabels[iter], multiValues[iter]);
m2.put(multiValues[iter], multiLabels[iter]);
}
cnt.add(b.getLabel()).add(stringPicker);
allBindings.add(uib.bind(b, stringPicker, new UiBinding.PickerAdapter<Object>(new UiBinding.MappingConverter(m1), new UiBinding.MappingConverter(m2))));
}
continue;
}
Class t = b.getGenericType();
if (t != null) {
if (t == Boolean.class) {
CheckBox cb = new CheckBox();
uib.bind(b, cb);
cnt.add(b.getLabel()).add(cb);
continue;
}
if (t == Date.class) {
Picker dp = new Picker();
dp.setType(Display.PICKER_TYPE_DATE);
uib.bind(b, dp);
cnt.add(b.getLabel()).add(dp);
continue;
}
}
TextField tf = new TextField();
tf.setConstraint(getTextFieldConstraint(b));
uib.bind(b, tf);
cnt.add(b.getLabel()).add(tf);
}
cnt.putClientProperty("cn1$iui-binding", uib.createGroupBinding(allBindings));
return cnt;
}
use of com.codename1.properties.PropertyBusinessObject in project CodenameOne by codenameone.
the class Util method readObject.
/**
* <p>Reads an object from the stream, notice that this is the inverse of the
* {@link #writeObject(java.lang.Object, java.io.DataOutputStream)}.</p>
*
* <p>
* The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
* </p>
* <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
*
* @param input the source input stream
* @throws IOException thrown by the stream
*/
public static Object readObject(DataInputStream input) throws IOException {
try {
if (!input.readBoolean()) {
return null;
}
String type = input.readUTF();
if ("int".equals(type)) {
return new Integer(input.readInt());
}
if ("byte".equals(type)) {
return new Byte(input.readByte());
}
if ("short".equals(type)) {
return new Short(input.readShort());
}
if ("long".equals(type)) {
return new Long(input.readLong());
}
if ("float".equals(type)) {
return new Float(input.readFloat());
}
if ("double".equals(type)) {
return new Double(input.readDouble());
}
if ("bool".equals(type)) {
return new Boolean(input.readBoolean());
}
if ("String".equals(type)) {
return input.readUTF();
}
if ("Date".equals(type)) {
return new Date(input.readLong());
}
if ("ObjectArray".equals(type)) {
Object[] v = new Object[input.readInt()];
int vlen = v.length;
for (int iter = 0; iter < vlen; iter++) {
v[iter] = readObject(input);
}
return v;
}
if ("ByteArray".equals(type)) {
byte[] v = new byte[input.readInt()];
input.readFully(v);
return v;
}
if ("LongArray".equals(type)) {
long[] v = new long[input.readInt()];
int vlen = v.length;
for (int iter = 0; iter < vlen; iter++) {
v[iter] = input.readLong();
}
return v;
}
if ("ShortArray".equals(type)) {
short[] v = new short[input.readInt()];
int vlen = v.length;
for (int iter = 0; iter < vlen; iter++) {
v[iter] = input.readShort();
}
return v;
}
if ("DoubleArray".equals(type)) {
double[] v = new double[input.readInt()];
int vlen = v.length;
for (int iter = 0; iter < vlen; iter++) {
v[iter] = input.readDouble();
}
return v;
}
if ("FloatArray".equals(type)) {
float[] v = new float[input.readInt()];
int vlen = v.length;
for (int iter = 0; iter < vlen; iter++) {
v[iter] = input.readFloat();
}
return v;
}
if ("IntArray".equals(type)) {
int[] v = new int[input.readInt()];
int vlen = v.length;
for (int iter = 0; iter < vlen; iter++) {
v[iter] = input.readInt();
}
return v;
}
if ("java.util.Vector".equals(type)) {
Vector v = new Vector();
int size = input.readInt();
for (int iter = 0; iter < size; iter++) {
v.addElement(readObject(input));
}
return v;
}
if ("java.util.Hashtable".equals(type)) {
Hashtable v = new Hashtable();
int size = input.readInt();
for (int iter = 0; iter < size; iter++) {
v.put(readObject(input), readObject(input));
}
return v;
}
if ("java.util.Collection".equals(type)) {
Collection v = new ArrayList();
int size = input.readInt();
for (int iter = 0; iter < size; iter++) {
v.add(readObject(input));
}
return v;
}
if ("java.util.Map".equals(type)) {
Map v = new HashMap();
int size = input.readInt();
for (int iter = 0; iter < size; iter++) {
v.put(readObject(input), readObject(input));
}
return v;
}
if ("EncodedImage".equals(type)) {
int width = input.readInt();
int height = input.readInt();
boolean op = input.readBoolean();
byte[] data = new byte[input.readInt()];
input.readFully(data);
return EncodedImage.create(data, width, height, op);
}
Class cls = (Class) externalizables.get(type);
if (cls != null) {
Object o = cls.newInstance();
if (o instanceof Externalizable) {
Externalizable ex = (Externalizable) o;
ex.internalize(input.readInt(), input);
return ex;
} else {
PropertyBusinessObject pb = (PropertyBusinessObject) o;
pb.getPropertyIndex().asExternalizable().internalize(input.readInt(), input);
return pb;
}
}
throw new IOException("Object type not supported: " + type);
} catch (InstantiationException ex1) {
Log.e(ex1);
throw new IOException(ex1.getClass().getName() + ": " + ex1.getMessage());
} catch (IllegalAccessException ex1) {
Log.e(ex1);
throw new IOException(ex1.getClass().getName() + ": " + ex1.getMessage());
}
}
use of com.codename1.properties.PropertyBusinessObject in project CodenameOne by codenameone.
the class Util method writeObject.
/**
* <p>Writes an object to the given output stream, notice that it should be externalizable or one of
* the supported types.</p>
*
* <p>
* The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
* </p>
* <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
*
* @param o the object to write which can be null
* @param out the destination output stream
* @throws IOException thrown by the stream
*/
public static void writeObject(Object o, DataOutputStream out) throws IOException {
if (o == null) {
out.writeBoolean(false);
return;
}
out.writeBoolean(true);
if (o instanceof Externalizable) {
Externalizable e = (Externalizable) o;
out.writeUTF(e.getObjectId());
out.writeInt(e.getVersion());
e.externalize(out);
return;
}
if (o instanceof PropertyBusinessObject) {
Externalizable e = ((PropertyBusinessObject) o).getPropertyIndex().asExternalizable();
out.writeUTF(e.getObjectId());
out.writeInt(e.getVersion());
e.externalize(out);
return;
}
if (o instanceof Vector) {
Vector v = (Vector) o;
out.writeUTF("java.util.Vector");
int size = v.size();
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
writeObject(v.elementAt(iter), out);
}
return;
}
if (o instanceof Collection) {
Collection v = (Collection) o;
out.writeUTF("java.util.Collection");
int size = v.size();
out.writeInt(size);
for (Object cur : v) {
writeObject(cur, out);
}
return;
}
if (o instanceof Hashtable) {
Hashtable v = (Hashtable) o;
out.writeUTF("java.util.Hashtable");
out.writeInt(v.size());
Enumeration k = v.keys();
while (k.hasMoreElements()) {
Object key = k.nextElement();
writeObject(key, out);
writeObject(v.get(key), out);
}
return;
}
if (o instanceof Map) {
Map v = (Map) o;
out.writeUTF("java.util.Map");
out.writeInt(v.size());
for (Object key : v.keySet()) {
writeObject(key, out);
writeObject(v.get(key), out);
}
return;
}
if (o instanceof String) {
String v = (String) o;
out.writeUTF("String");
out.writeUTF(v);
return;
}
if (o instanceof Date) {
Date v = (Date) o;
out.writeUTF("Date");
out.writeLong(v.getTime());
return;
}
if (o instanceof Integer) {
Integer v = (Integer) o;
out.writeUTF("int");
out.writeInt(v.intValue());
return;
}
if (o instanceof Long) {
Long v = (Long) o;
out.writeUTF("long");
out.writeLong(v.longValue());
return;
}
if (o instanceof Byte) {
Byte v = (Byte) o;
out.writeUTF("byte");
out.writeByte(v.byteValue());
return;
}
if (o instanceof Short) {
Short v = (Short) o;
out.writeUTF("short");
out.writeShort(v.shortValue());
return;
}
if (o instanceof Float) {
Float v = (Float) o;
out.writeUTF("float");
out.writeFloat(v.floatValue());
return;
}
if (o instanceof Double) {
Double v = (Double) o;
out.writeUTF("double");
out.writeDouble(v.doubleValue());
return;
}
if (o instanceof Boolean) {
Boolean v = (Boolean) o;
out.writeUTF("bool");
out.writeBoolean(v.booleanValue());
return;
}
if (o instanceof EncodedImage) {
out.writeUTF("EncodedImage");
EncodedImage e = (EncodedImage) o;
out.writeInt(e.getWidth());
out.writeInt(e.getHeight());
out.writeBoolean(e.isOpaque());
byte[] b = e.getImageData();
out.writeInt(b.length);
out.write(b);
return;
}
if (instanceofObjArray(o)) {
Object[] v = (Object[]) o;
out.writeUTF("ObjectArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
writeObject(v[iter], out);
}
return;
}
if (instanceofByteArray(o)) {
byte[] v = (byte[]) o;
out.writeUTF("ByteArray");
int size = v.length;
out.writeInt(size);
out.write(v);
return;
}
if (instanceofShortArray(o)) {
short[] v = (short[]) o;
out.writeUTF("ShortArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeShort(v[iter]);
}
return;
}
if (instanceofDoubleArray(o)) {
double[] v = (double[]) o;
out.writeUTF("DoubleArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeDouble(v[iter]);
}
return;
}
if (instanceofFloatArray(o)) {
float[] v = (float[]) o;
out.writeUTF("FloatArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeFloat(v[iter]);
}
return;
}
if (instanceofIntArray(o)) {
int[] v = (int[]) o;
out.writeUTF("IntArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeInt(v[iter]);
}
return;
}
if (instanceofLongArray(o)) {
long[] v = (long[]) o;
out.writeUTF("LongArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeLong(v[iter]);
}
return;
}
throw new IOException("Object type not supported: " + o.getClass().getName() + " value: " + o);
}
use of com.codename1.properties.PropertyBusinessObject in project CodenameOne by codenameone.
the class SQLMap method select.
/**
* 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 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
*/
public java.util.List<PropertyBusinessObject> select(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));
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());
}
}
}
Aggregations