2016-03-27 22:13:06 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.daogen;
|
|
|
|
|
|
|
|
import de.greenrobot.daogenerator.DaoGenerator;
|
|
|
|
import de.greenrobot.daogenerator.Entity;
|
2016-06-19 00:39:58 +02:00
|
|
|
import de.greenrobot.daogenerator.Index;
|
2016-03-27 22:13:06 +02:00
|
|
|
import de.greenrobot.daogenerator.Property;
|
|
|
|
import de.greenrobot.daogenerator.Schema;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates entities and DAOs for the example project DaoExample.
|
|
|
|
* Automatically run during build.
|
|
|
|
*/
|
|
|
|
public class GBDaoGenerator {
|
|
|
|
|
|
|
|
public static final String VALID_FROM_UTC = "validFromUTC";
|
|
|
|
public static final String VALID_TO_UTC = "validToUTC";
|
2016-05-01 00:19:15 +02:00
|
|
|
private static final String MAIN_PACKAGE = "nodomain.freeyourgadget.gadgetbridge";
|
|
|
|
private static final String MODEL_PACKAGE = MAIN_PACKAGE + ".model";
|
2016-06-06 23:18:46 +02:00
|
|
|
private static final String VALID_BY_DATE = MODEL_PACKAGE + ".ValidByDate";
|
2016-03-27 22:13:06 +02:00
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
2016-07-27 23:34:13 +02:00
|
|
|
Schema schema = new Schema(9, MAIN_PACKAGE + ".entities");
|
2016-03-27 22:13:06 +02:00
|
|
|
|
2016-05-23 23:31:22 +02:00
|
|
|
addActivityDescription(schema);
|
|
|
|
|
2016-03-27 22:13:06 +02:00
|
|
|
Entity userAttributes = addUserAttributes(schema);
|
|
|
|
Entity user = addUserInfo(schema, userAttributes);
|
|
|
|
|
|
|
|
Entity deviceAttributes = addDeviceAttributes(schema);
|
|
|
|
Entity device = addDevice(schema, deviceAttributes);
|
|
|
|
|
2016-04-17 19:52:51 +02:00
|
|
|
addMiBandActivitySample(schema, user, device);
|
|
|
|
addPebbleActivitySample(schema, user, device);
|
2016-07-25 22:19:39 +02:00
|
|
|
addPebbleMisfitActivitySample(schema, user, device);
|
2016-03-27 22:13:06 +02:00
|
|
|
|
2016-04-09 18:18:16 +02:00
|
|
|
new DaoGenerator().generateAll(schema, "app/src/main/java");
|
2016-03-27 22:13:06 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 23:31:22 +02:00
|
|
|
private static Entity addActivityDescription(Schema schema) {
|
|
|
|
Entity activityDescription = addEntity(schema, "ActivityDescription");
|
|
|
|
activityDescription.addIdProperty();
|
|
|
|
activityDescription.addIntProperty("fromTimestamp").notNull();
|
|
|
|
activityDescription.addIntProperty("toTimestamp");
|
|
|
|
return activityDescription;
|
|
|
|
}
|
|
|
|
|
2016-03-27 22:13:06 +02:00
|
|
|
private static Entity addUserInfo(Schema schema, Entity userAttributes) {
|
2016-05-01 00:19:15 +02:00
|
|
|
Entity user = addEntity(schema, "User");
|
2016-03-27 22:13:06 +02:00
|
|
|
user.addIdProperty();
|
|
|
|
user.addStringProperty("name").notNull();
|
|
|
|
user.addDateProperty("birthday").notNull();
|
2016-05-13 23:47:47 +02:00
|
|
|
user.addIntProperty("gender").notNull();
|
2016-03-27 22:13:06 +02:00
|
|
|
Property userId = userAttributes.addLongProperty("userId").notNull().getProperty();
|
2016-05-23 23:31:22 +02:00
|
|
|
|
|
|
|
// sorted by the from-date, newest first
|
|
|
|
Property userAttributesSortProperty = getPropertyByName(userAttributes, VALID_FROM_UTC);
|
|
|
|
user.addToMany(userAttributes, userId).orderDesc(userAttributesSortProperty);
|
2016-03-27 22:13:06 +02:00
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:31:22 +02:00
|
|
|
private static Property getPropertyByName(Entity entity, String propertyName) {
|
|
|
|
for (Property prop : entity.getProperties()) {
|
|
|
|
if (propertyName.equals(prop.getPropertyName())) {
|
|
|
|
return prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalStateException("Could not find property " + propertyName + " in entity " + entity.getClassName());
|
|
|
|
}
|
|
|
|
|
2016-03-27 22:13:06 +02:00
|
|
|
private static Entity addUserAttributes(Schema schema) {
|
|
|
|
// additional properties of a user, which may change during the lifetime of a user
|
|
|
|
// this allows changing attributes while preserving user identity
|
2016-05-01 00:19:15 +02:00
|
|
|
Entity userAttributes = addEntity(schema, "UserAttributes");
|
2016-03-27 22:13:06 +02:00
|
|
|
userAttributes.addIdProperty();
|
|
|
|
userAttributes.addIntProperty("heightCM").notNull();
|
|
|
|
userAttributes.addIntProperty("weightKG").notNull();
|
|
|
|
userAttributes.addIntProperty("sleepGoalHPD");
|
|
|
|
userAttributes.addIntProperty("stepsGoalSPD");
|
2016-06-06 23:18:46 +02:00
|
|
|
addDateValidityTo(userAttributes);
|
2016-03-27 22:13:06 +02:00
|
|
|
|
|
|
|
return userAttributes;
|
|
|
|
}
|
|
|
|
|
2016-06-06 23:18:46 +02:00
|
|
|
private static void addDateValidityTo(Entity entity) {
|
|
|
|
entity.addDateProperty(VALID_FROM_UTC);
|
|
|
|
entity.addDateProperty(VALID_TO_UTC);
|
|
|
|
|
|
|
|
entity.implementsInterface(VALID_BY_DATE);
|
|
|
|
}
|
|
|
|
|
2016-03-27 22:13:06 +02:00
|
|
|
private static Entity addDevice(Schema schema, Entity deviceAttributes) {
|
2016-05-01 00:19:15 +02:00
|
|
|
Entity device = addEntity(schema, "Device");
|
2016-03-27 22:13:06 +02:00
|
|
|
device.addIdProperty();
|
|
|
|
device.addStringProperty("name").notNull();
|
|
|
|
device.addStringProperty("manufacturer").notNull();
|
2016-05-13 23:47:47 +02:00
|
|
|
device.addStringProperty("identifier").notNull().unique().javaDocGetterAndSetter("The fixed identifier, i.e. MAC address of the device.");
|
2016-03-27 22:13:06 +02:00
|
|
|
Property deviceId = deviceAttributes.addLongProperty("deviceId").notNull().getProperty();
|
2016-05-23 23:31:22 +02:00
|
|
|
// sorted by the from-date, newest first
|
|
|
|
Property deviceAttributesSortProperty = getPropertyByName(deviceAttributes, VALID_FROM_UTC);
|
|
|
|
device.addToMany(deviceAttributes, deviceId).orderDesc(deviceAttributesSortProperty);
|
2016-03-27 22:13:06 +02:00
|
|
|
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Entity addDeviceAttributes(Schema schema) {
|
2016-05-01 00:19:15 +02:00
|
|
|
Entity deviceAttributes = addEntity(schema, "DeviceAttributes");
|
2016-03-27 22:13:06 +02:00
|
|
|
deviceAttributes.addIdProperty();
|
|
|
|
deviceAttributes.addStringProperty("firmwareVersion1").notNull();
|
|
|
|
deviceAttributes.addStringProperty("firmwareVersion2");
|
2016-06-06 23:18:46 +02:00
|
|
|
addDateValidityTo(deviceAttributes);
|
2016-03-27 22:13:06 +02:00
|
|
|
|
|
|
|
return deviceAttributes;
|
|
|
|
}
|
|
|
|
|
2016-04-17 19:52:51 +02:00
|
|
|
private static Entity addMiBandActivitySample(Schema schema, Entity user, Entity device) {
|
2016-03-27 22:13:06 +02:00
|
|
|
// public GBActivitySample(SampleProvider provider, int timestamp, int intensity, int steps, int type, int customValue) {
|
2016-05-01 00:19:15 +02:00
|
|
|
Entity activitySample = addEntity(schema, "MiBandActivitySample");
|
2016-07-25 22:19:39 +02:00
|
|
|
addCommonActivitySampleProperties("AbstractActivitySample", activitySample, user, device);
|
|
|
|
addDefaultActivitySampleAttributes(activitySample);
|
|
|
|
addCommonActivitySampleProperties2(activitySample, user, device);
|
2016-05-13 23:47:47 +02:00
|
|
|
addHeartRateProperties(activitySample);
|
|
|
|
return activitySample;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void addHeartRateProperties(Entity activitySample) {
|
2016-07-27 23:34:13 +02:00
|
|
|
activitySample.addIntProperty("heartRate").notNull();
|
2016-04-17 19:52:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Entity addPebbleActivitySample(Schema schema, Entity user, Entity device) {
|
|
|
|
// public GBActivitySample(SampleProvider provider, int timestamp, int intensity, int steps, int type, int customValue) {
|
2016-05-01 00:19:15 +02:00
|
|
|
Entity activitySample = addEntity(schema, "PebbleActivitySample");
|
2016-07-25 22:19:39 +02:00
|
|
|
addCommonActivitySampleProperties("AbstractActivitySample", activitySample, user, device);
|
|
|
|
addDefaultActivitySampleAttributes(activitySample);
|
|
|
|
addCommonActivitySampleProperties2(activitySample, user, device);
|
2016-04-17 19:52:51 +02:00
|
|
|
return activitySample;
|
|
|
|
}
|
|
|
|
|
2016-07-25 22:19:39 +02:00
|
|
|
private static Entity addPebbleMisfitActivitySample(Schema schema, Entity user, Entity device) {
|
|
|
|
Entity activitySample = addEntity(schema, "PebbleMisfitSample");
|
|
|
|
addCommonActivitySampleProperties("AbstractPebbleMisfitActivitySample", activitySample, user, device);
|
|
|
|
activitySample.addIntProperty("rawPebbleMisfitSample").notNull();
|
|
|
|
addCommonActivitySampleProperties2(activitySample, user, device);
|
|
|
|
return activitySample;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void addCommonActivitySampleProperties(String superClass, Entity activitySample, Entity user, Entity device) {
|
|
|
|
activitySample.setSuperclass(superClass);
|
2016-05-01 00:19:15 +02:00
|
|
|
activitySample.addImport(MAIN_PACKAGE + ".devices.SampleProvider");
|
|
|
|
activitySample.setJavaDoc(
|
|
|
|
"This class represents a sample specific to the device. Values like activity kind or\n" +
|
|
|
|
"intensity, are device specific. Normalized values can be retrieved through the\n" +
|
|
|
|
"corresponding {@link SampleProvider}.");
|
2016-03-27 22:13:06 +02:00
|
|
|
activitySample.addIdProperty();
|
2016-07-28 22:12:20 +02:00
|
|
|
activitySample.addIntProperty("timestamp").notNull();
|
2016-07-25 22:19:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void addCommonActivitySampleProperties2(Entity activitySample, Entity user, Entity device) {
|
2016-03-27 22:13:06 +02:00
|
|
|
Property userId = activitySample.addLongProperty("userId").getProperty();
|
|
|
|
activitySample.addToOne(user, userId);
|
|
|
|
Property deviceId = activitySample.addLongProperty("deviceId").getProperty();
|
|
|
|
activitySample.addToOne(device, deviceId);
|
2016-06-19 00:39:58 +02:00
|
|
|
|
|
|
|
Index indexUnique = new Index();
|
2016-07-28 22:12:20 +02:00
|
|
|
indexUnique.addProperty(findProperty(activitySample, "timestamp"));
|
2016-06-19 00:39:58 +02:00
|
|
|
indexUnique.addProperty(deviceId);
|
|
|
|
indexUnique.makeUnique();
|
|
|
|
activitySample.addIndex(indexUnique);
|
2016-03-27 22:13:06 +02:00
|
|
|
}
|
2016-07-25 22:19:39 +02:00
|
|
|
|
2016-07-28 22:12:20 +02:00
|
|
|
private static Property findProperty(Entity entity, String propertyName) {
|
|
|
|
for (Property prop : entity.getProperties()) {
|
|
|
|
if (propertyName.equals(prop.getPropertyName())) {
|
|
|
|
return prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Property " + propertyName + " not found in Entity " + entity.getClassName());
|
|
|
|
}
|
|
|
|
|
2016-07-25 22:19:39 +02:00
|
|
|
private static void addDefaultActivitySampleAttributes(Entity activitySample) {
|
|
|
|
activitySample.addIntProperty("rawIntensity").notNull();
|
|
|
|
activitySample.addIntProperty("steps").notNull();
|
|
|
|
activitySample.addIntProperty("rawKind").notNull();
|
|
|
|
}
|
|
|
|
|
2016-05-01 00:19:15 +02:00
|
|
|
private static Entity addEntity(Schema schema, String className) {
|
|
|
|
Entity entity = schema.addEntity(className);
|
|
|
|
entity.addImport("de.greenrobot.dao.AbstractDao");
|
|
|
|
return entity;
|
|
|
|
}
|
2016-03-27 22:13:06 +02:00
|
|
|
}
|