private void writeObject(ObjectOutputStream out) throws IOException { if(value == null) { out.writeBoolean(false); } else { // Avoid writeUTF as it has a limit of 65k out.writeBoolean(true); byte[] bytes = value.toJsonString().getBytes(Charsets.UTF_8); out.writeInt(bytes.length); out.write(bytes); } } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
/** * Updates a Form's Schema * * Updates the form * * @param formId Id of the Form * @param updatedSchema Updates the schema describing this form's fields */ public Promise<Void> updateFormSchema(String formId, FormClass updatedSchema) { return post(RequestBuilder.PUT, schemaUrl(formId), updatedSchema.toJsonString()); }
public void createForm(FormClass formClass) { client.resource(root) .path("resources") .path("form") .path(formClass.getId().asString()) .path("schema") .type(MediaType.APPLICATION_JSON_TYPE) .put(formClass.toJsonString()); }
@Test public void serializationTest() throws IOException, ClassNotFoundException { // Large form class FormClass formClass = new FormClass(ResourceId.generateId()); formClass.setLabel("Widget survey"); for (int i = 0; i < 1000; i++) { formClass.addField(ResourceId.generateId()) .setLabel("How many widgets?") .setType(new QuantityType("people")); } assertTrue(formClass.toJsonString().length() > 0xFFFF); Activity activity = new Activity(); activity.serializedFormClass = new Activity.FormClassHolder(); activity.serializedFormClass.value = formClass; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(activity); // Re-read ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); Activity reread = (Activity) ois.readObject(); assertThat(reread.getSerializedFormClass().toJsonString(), equalTo(formClass.toJsonString())); }
public void insert(FormClass formClass) { Preconditions.checkArgument(CuidAdapter.getLegacyIdFromCuid(formClass.getId()) == activityId); // find the nullary location type id int locationTypeId; try(ResultSet rs = executor.query("SELECT locationTypeId FROM locationtype " + "WHERE nullary=1 AND " + "countryId = (SELECT countryId FROM userdatabase WHERE databaseId=" + databaseId + ")")) { if(!rs.next()) { throw new IllegalStateException("Cannot find nullary location type for database " + databaseId); } locationTypeId = rs.getInt(1); } catch (SQLException e) { throw new RuntimeException("Failed to query location type", e); } SqlInsert insert = SqlInsert.insertInto("activity"); insert.value("activityId", activityId); insert.value("databaseId", databaseId); insert.value("name", formClass.getLabel(), 255); insert.value("formClass", validateNewFormClass(formClass).toJsonString()); insert.value("version", newVersion); insert.value("schemaVersion", newVersion); insert.value("classicView", 0); insert.value("locationTypeId", locationTypeId); insert.value("allowEdit", 1); insert.value("reportingFrequency", 0); insert.value("sortOrder", 0); insert.execute(executor); }
private void updateActivityRow(FormClass formClass) { SqlUpdate activityUpdate = SqlUpdate.update("activity"); activityUpdate.where("activityId", activityId); activityUpdate.setIfChanged("name", activityName, formClass.getLabel(), 255); activityUpdate.set("formClass", formClass.toJsonString()); activityUpdate.set("gzFormClass", (String)null); activityUpdate.set("version", newVersion); activityUpdate.set("schemaVersion", newVersion); // TODO: location type activityUpdate.execute(executor); }