use of org.commongeoregistry.adapter.metadata.AttributeDateType in project geoprism-registry by terraframe.
the class UpdateValueOverTimeView method persistValue.
private void persistValue(ValueOverTime vot, UpdateChangeOverTimeAttributeView cotView, VertexServerGeoObject go, List<ValueOverTime> looseVotc) {
if (this.newValue == null) {
return;
}
if (cotView.getAttributeName().equals("geometry")) {
Geometry convertedValue = null;
if (!this.newValue.isJsonNull()) {
GeoJSONReader reader = new GeoJSONReader();
convertedValue = reader.read(this.newValue.toString());
if (!go.isValidGeometry(convertedValue)) {
GeometryTypeException ex = new GeometryTypeException();
ex.setActualType(convertedValue.getGeometryType());
ex.setExpectedType(go.getType().getGeometryType().name());
throw ex;
}
}
if (vot != null) {
vot.setValue(convertedValue);
} else {
looseVotc.add(new ValueOverTime(this.newStartDate, this.newEndDate, convertedValue));
}
} else {
ServerGeoObjectType type = go.getType();
AttributeType attype = type.getAttribute(cotView.getAttributeName()).get();
if (attype instanceof AttributeLocalType) {
LocalizedValue convertedValue = null;
if (!this.newValue.isJsonNull()) {
convertedValue = LocalizedValue.fromJSON(this.newValue.getAsJsonObject());
}
final Set<Locale> locales = LocalizationFacade.getInstalledLocales();
if (vot != null) {
if (convertedValue != null) {
GraphObjectDAO votEmbeddedValue = (GraphObjectDAO) vot.getValue();
votEmbeddedValue.setValue(MdAttributeLocalInfo.DEFAULT_LOCALE, convertedValue.getValue(MdAttributeLocalInfo.DEFAULT_LOCALE));
for (Locale locale : locales) {
if (convertedValue.contains(locale)) {
votEmbeddedValue.setValue(locale.toString(), convertedValue.getValue(locale));
}
}
} else {
vot.setValue(null);
}
} else {
if (convertedValue != null) {
MdAttributeEmbeddedDAOIF mdAttrEmbedded = (MdAttributeEmbeddedDAOIF) go.getMdAttributeDAO(attype.getName());
VertexObjectDAO votEmbeddedValue = VertexObjectDAO.newInstance((MdVertexDAOIF) mdAttrEmbedded.getEmbeddedMdClassDAOIF());
votEmbeddedValue.setValue(MdAttributeLocalInfo.DEFAULT_LOCALE, convertedValue.getValue(MdAttributeLocalInfo.DEFAULT_LOCALE));
for (Locale locale : locales) {
if (convertedValue.contains(locale)) {
votEmbeddedValue.setValue(locale.toString(), convertedValue.getValue(locale));
}
}
looseVotc.add(new ValueOverTime(this.newStartDate, this.newEndDate, votEmbeddedValue));
} else {
looseVotc.add(new ValueOverTime(this.newStartDate, this.newEndDate, null));
}
}
} else // else if (attype.getName().equals(DefaultAttribute.EXISTS.getName()))
// {
// if (this.newValue.isJsonNull())
// {
// if (vot != null)
// {
// vot.setValue(null);
// }
// else
// {
// looseVotc.add(new ValueOverTime(this.newStartDate, this.newEndDate,
// null));
// }
// }
// else
// {
// JsonArray ja = this.newValue.getAsJsonArray();
//
// if (ja.size() > 0)
// {
// String code = ja.get(0).getAsString();
//
// if (code == null || code.length() == 0)
// {
// if (vot != null)
// {
// vot.setValue(null);
// }
// else
// {
// looseVotc.add(new ValueOverTime(this.newStartDate, this.newEndDate,
// null));
// }
// }
// else
// {
// Term value = ( (AttributeTermType) attype ).getTermByCode(code).get();
// GeoObjectStatus gos =
// ConversionService.getInstance().termToGeoObjectStatus(value);
//
// if (vot != null)
// {
// vot.setValue(gos.getOid());
// }
// else
// {
// looseVotc.add(new ValueOverTime(this.newStartDate, this.newEndDate,
// gos.getOid()));
// }
// }
// }
// }
// }
{
Object convertedValue = null;
if (!this.newValue.isJsonNull()) {
if (attype instanceof AttributeDateType) {
long epoch = this.newValue.getAsLong();
convertedValue = new Date(epoch);
} else if (attype instanceof AttributeTermType) {
JsonArray ja = this.newValue.getAsJsonArray();
if (ja.size() > 0) {
String code = ja.get(0).getAsString();
Term root = ((AttributeTermType) attype).getRootTerm();
String parent = TermConverter.buildClassifierKeyFromTermCode(root.getCode());
String classifierKey = Classifier.buildKey(parent, code);
Classifier classifier = Classifier.getByKey(classifierKey);
convertedValue = classifier.getOid();
}
} else if (attype instanceof AttributeClassificationType) {
JsonObject object = this.newValue.getAsJsonObject();
String code = object.get("code").getAsString();
Classification classification = Classification.get((AttributeClassificationType) attype, code);
convertedValue = new AttributeGraphRef.ID(classification.getOid(), classification.getVertex().getRID());
} else if (attype instanceof AttributeBooleanType) {
convertedValue = this.newValue.getAsBoolean();
} else if (attype instanceof AttributeFloatType) {
convertedValue = this.newValue.getAsDouble();
} else if (attype instanceof AttributeIntegerType) {
convertedValue = this.newValue.getAsLong();
} else {
convertedValue = this.newValue.getAsString();
}
}
if (vot != null) {
vot.setValue(convertedValue);
} else {
looseVotc.add(new ValueOverTime(this.newStartDate, this.newEndDate, convertedValue));
}
}
}
}
use of org.commongeoregistry.adapter.metadata.AttributeDateType in project geoprism-registry by terraframe.
the class XMLImporter method addAttributes.
private void addAttributes(Element root, ServerGeoObjectType type) {
NodeList attributeList = root.getElementsByTagName("attributes");
if (attributeList.getLength() > 0) {
NodeList nList = attributeList.item(0).getChildNodes();
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element elem = (Element) nNode;
String code = elem.getAttribute("code");
LocalizedValue label = this.getLabel(elem);
LocalizedValue description = this.getDescription(elem);
if (elem.getTagName().equals("text")) {
type.createAttributeType(new AttributeCharacterType(code, label, description, false, false, false));
} else if (elem.getTagName().equals("boolean")) {
type.createAttributeType(new AttributeBooleanType(code, label, description, false, false, false));
} else if (elem.getTagName().equals("integer")) {
type.createAttributeType(new AttributeIntegerType(code, label, description, false, false, false));
} else if (elem.getTagName().equals("decimal")) {
AttributeFloatType attributeType = new AttributeFloatType(code, label, description, false, false, false);
attributeType.setPrecision(this.getPrecision(elem));
attributeType.setScale(this.getScale(elem));
type.createAttributeType(attributeType);
} else if (elem.getTagName().equals("date")) {
type.createAttributeType(new AttributeDateType(code, label, description, false, false, false));
} else if (elem.getTagName().equals("term")) {
AttributeTermType attributeType = new AttributeTermType(code, label, description, false, false, false);
attributeType = (AttributeTermType) type.createAttributeType(attributeType);
Term rootTerm = attributeType.getRootTerm();
this.createTermOptions(elem, rootTerm);
} else if (elem.getTagName().equals("classification")) {
String rootCode = elem.getAttribute("root");
String classificationType = elem.getAttribute("classificationType");
AttributeClassificationType attributeType = new AttributeClassificationType(code, label, description, false, false, false);
attributeType.setRootTerm(new Term(rootCode, new LocalizedValue(""), new LocalizedValue("")));
attributeType.setClassificationType(classificationType);
attributeType = (AttributeClassificationType) type.createAttributeType(attributeType);
}
}
}
}
}
use of org.commongeoregistry.adapter.metadata.AttributeDateType in project geoprism-registry by terraframe.
the class ListType method getRestriction.
public BasicVertexRestriction getRestriction(ServerGeoObjectType type, Date forDate) {
String filterJson = this.getFilterJson();
if (filterJson != null && filterJson.length() > 0) {
JsonArray filters = JsonParser.parseString(filterJson).getAsJsonArray();
CompositeRestriction restriction = new CompositeRestriction();
for (int i = 0; i < filters.size(); i++) {
JsonObject filter = filters.get(i).getAsJsonObject();
String attributeName = filter.get("attribute").getAsString();
String operation = filter.get("operation").getAsString();
AttributeType attributeType = type.getAttribute(attributeName).get();
MdAttributeDAOIF mdAttribute = type.getMdVertex().definesAttribute(attributeName);
if (attributeType instanceof AttributeDateType) {
String value = filter.get("value").getAsString();
Date date = GeoRegistryUtil.parseDate(value, false);
restriction.add(new AttributeValueRestriction(mdAttribute, operation, date, forDate));
} else if (attributeType instanceof AttributeBooleanType) {
String value = filter.get("value").getAsString();
Boolean bVal = Boolean.valueOf(value);
restriction.add(new AttributeValueRestriction(mdAttribute, operation, bVal, forDate));
} else if (attributeType instanceof AttributeTermType) {
String code = filter.get("value").getAsString();
Term root = ((AttributeTermType) attributeType).getRootTerm();
String parent = TermConverter.buildClassifierKeyFromTermCode(root.getCode());
String classifierKey = Classifier.buildKey(parent, code);
Classifier classifier = Classifier.getByKey(classifierKey);
restriction.add(new AttributeValueRestriction(mdAttribute, operation, classifier.getOid(), forDate));
} else if (attributeType instanceof AttributeClassificationType) {
JsonObject object = filter.get("value").getAsJsonObject();
Term term = Term.fromJSON(object);
MdClassificationDAOIF mdClassification = ((MdAttributeClassificationDAOIF) mdAttribute).getMdClassificationDAOIF();
MdEdgeDAOIF mdEdge = mdClassification.getReferenceMdEdgeDAO();
ClassificationType classificationType = new ClassificationType(mdClassification);
Classification classification = Classification.get(classificationType, term.getCode());
restriction.add(new AttributeValueRestriction(mdAttribute, operation, classification.getVertex().getRID(), forDate));
} else {
String value = filter.get("value").getAsString();
restriction.add(new AttributeValueRestriction(mdAttribute, operation, value, forDate));
}
}
if (restriction.getRestrictions().size() > 0) {
return restriction;
}
}
return null;
}
use of org.commongeoregistry.adapter.metadata.AttributeDateType in project geoprism-registry by terraframe.
the class DHIS2FeatureService method getDHIS2AttributeConfiguration.
@Request(RequestType.SESSION)
public JsonArray getDHIS2AttributeConfiguration(String sessionId, String dhis2SystemOid, String geoObjectTypeCode) {
DHIS2ExternalSystem system = DHIS2ExternalSystem.get(dhis2SystemOid);
JsonArray response = new JsonArray();
ServerGeoObjectType got = ServerGeoObjectType.get(geoObjectTypeCode);
Map<String, AttributeType> cgrAttrs = got.getAttributeMap();
DHIS2TransportServiceIF dhis2;
try {
dhis2 = DHIS2ServiceFactory.buildDhis2TransportService(system);
} catch (InvalidLoginException e) {
LoginException cgrlogin = new LoginException(e);
throw cgrlogin;
} catch (HTTPException | UnexpectedResponseException | BadServerUriException | IllegalArgumentException e) {
HttpError cgrhttp = new HttpError(e);
throw cgrhttp;
}
final DHIS2OptionCache optionCache = new DHIS2OptionCache(dhis2);
List<Attribute> dhis2Attrs = getDHIS2Attributes(dhis2);
final String[] skipAttrs = new String[] { DefaultAttribute.GEOMETRY.getName(), DefaultAttribute.SEQUENCE.getName(), DefaultAttribute.TYPE.getName() };
for (AttributeType cgrAttr : cgrAttrs.values()) {
if (!ArrayUtils.contains(skipAttrs, cgrAttr.getName())) {
JsonObject joAttr = new JsonObject();
JsonObject joCgrAttr = new JsonObject();
joCgrAttr.addProperty("name", cgrAttr.getName());
joCgrAttr.addProperty("label", cgrAttr.getLabel().getValue());
joCgrAttr.addProperty("type", cgrAttr.getType());
joCgrAttr.addProperty("typeLabel", AttributeTypeMetadata.get().getTypeEnumDisplayLabel(cgrAttr.getType()));
joAttr.add("cgrAttr", joCgrAttr);
JsonArray jaStrategies = new JsonArray();
List<DHIS2AttributeMapping> strategies = this.getMappingStrategies(cgrAttr);
for (DHIS2AttributeMapping strategy : strategies) {
jaStrategies.add(strategy.getClass().getName());
}
joAttr.add("attributeMappingStrategies", jaStrategies);
JsonArray jaDhis2Attrs = new JsonArray();
for (Attribute dhis2Attr : dhis2Attrs) {
if (!dhis2Attr.getOrganisationUnitAttribute()) {
continue;
}
boolean valid = false;
JsonObject jo = new JsonObject();
if (cgrAttr instanceof AttributeBooleanType && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.BOOLEAN) || dhis2Attr.getValueType().equals(ValueType.TRUE_ONLY))) {
valid = true;
} else if (cgrAttr instanceof AttributeIntegerType && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.INTEGER) || dhis2Attr.getValueType().equals(ValueType.INTEGER_POSITIVE) || dhis2Attr.getValueType().equals(ValueType.INTEGER_NEGATIVE) || dhis2Attr.getValueType().equals(ValueType.INTEGER_ZERO_OR_POSITIVE))) {
valid = true;
} else if (cgrAttr instanceof AttributeFloatType && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.NUMBER) || dhis2Attr.getValueType().equals(ValueType.UNIT_INTERVAL) || dhis2Attr.getValueType().equals(ValueType.PERCENTAGE))) {
valid = true;
} else if (cgrAttr instanceof AttributeDateType && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.DATE) || dhis2Attr.getValueType().equals(ValueType.DATETIME) || dhis2Attr.getValueType().equals(ValueType.TIME) || dhis2Attr.getValueType().equals(ValueType.AGE))) {
valid = true;
} else if (cgrAttr instanceof AttributeTermType && dhis2Attr.getOptionSetId() != null) {
valid = true;
JsonArray jaDhis2Options = new JsonArray();
IntegratedOptionSet set = optionCache.getOptionSet(dhis2Attr.getOptionSetId());
SortedSet<Option> options = set.getOptions();
for (Option option : options) {
JsonObject joDhis2Option = new JsonObject();
joDhis2Option.addProperty("code", option.getCode());
joDhis2Option.addProperty("name", option.getName());
joDhis2Option.addProperty("id", option.getName());
jaDhis2Options.add(joDhis2Option);
}
jo.add("options", jaDhis2Options);
} else if ((cgrAttr instanceof AttributeCharacterType || cgrAttr instanceof AttributeLocalType) && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.TEXT) || dhis2Attr.getValueType().equals(ValueType.LONG_TEXT) || dhis2Attr.getValueType().equals(ValueType.LETTER) || dhis2Attr.getValueType().equals(ValueType.PHONE_NUMBER) || dhis2Attr.getValueType().equals(ValueType.EMAIL) || dhis2Attr.getValueType().equals(ValueType.USERNAME) || dhis2Attr.getValueType().equals(ValueType.URL))) {
valid = true;
}
if (valid) {
jo.addProperty("dhis2Id", dhis2Attr.getId());
jo.addProperty("code", dhis2Attr.getCode());
jo.addProperty("name", dhis2Attr.getName());
jaDhis2Attrs.add(jo);
}
}
joAttr.add("dhis2Attrs", jaDhis2Attrs);
if (cgrAttr instanceof AttributeTermType) {
JsonArray terms = new JsonArray();
List<Term> children = ((AttributeTermType) cgrAttr).getTerms();
for (Term child : children) {
JsonObject joTerm = new JsonObject();
joTerm.addProperty("label", child.getLabel().getValue());
joTerm.addProperty("code", child.getCode());
terms.add(joTerm);
}
joAttr.add("terms", terms);
}
response.add(joAttr);
}
}
return response;
}
use of org.commongeoregistry.adapter.metadata.AttributeDateType in project geoprism-registry by terraframe.
the class DHIS2ServiceTest method exportCustomAttribute.
private void exportCustomAttribute(TestGeoObjectTypeInfo got, TestGeoObjectInfo go, TestAttributeTypeInfo attr) throws InterruptedException {
DHIS2SyncLevel level2 = new DHIS2SyncLevel();
level2.setGeoObjectType(got.getServerObject().getCode());
level2.setSyncType(DHIS2SyncLevel.Type.ALL);
level2.setLevel(1);
Collection<DHIS2AttributeMapping> mappings = getDefaultMappings();
DHIS2AttributeMapping mapping;
if (attr.getType().equals(AttributeTermType.TYPE)) {
mapping = new DHIS2TermAttributeMapping();
mapping.setAttributeMappingStrategy(DHIS2TermAttributeMapping.class.getName());
Map<String, String> terms = new HashMap<String, String>();
terms.put(AllAttributesDataset.AT_GO_TERM.fetchRootAsClassifier().getClassifierId(), "TEST_EXTERNAL_ID");
terms.put(AllAttributesDataset.TERM_TERM_VAL1.fetchClassifier().getClassifierId(), "TEST_EXTERNAL_ID");
terms.put(AllAttributesDataset.TERM_TERM_VAL2.fetchClassifier().getClassifierId(), "TEST_EXTERNAL_ID");
((DHIS2TermAttributeMapping) mapping).setTerms(terms);
} else {
mapping = new DHIS2AttributeMapping();
mapping.setAttributeMappingStrategy(DHIS2AttributeMapping.class.getName());
}
mapping.setCgrAttrName(attr.getAttributeName());
mapping.setDhis2AttrName(attr.getAttributeName());
mapping.setExternalId("TEST_EXTERNAL_ID");
mappings.add(mapping);
level2.setMappings(mappings);
SynchronizationConfig config = createSyncConfig(this.system, level2);
JsonObject jo = syncService.run(testData.clientSession.getSessionId(), config.getOid());
ExportHistory hist = ExportHistory.get(jo.get("historyId").getAsString());
SchedulerTestUtils.waitUntilStatus(hist.getOid(), AllJobStatus.SUCCESS);
LinkedList<Dhis2Payload> payloads = this.dhis2.getPayloads();
Assert.assertEquals(2, payloads.size());
for (int level = 0; level < payloads.size(); ++level) {
Dhis2Payload payload = payloads.get(level);
JsonObject joPayload = JsonParser.parseString(payload.getData()).getAsJsonObject();
JsonArray orgUnits = joPayload.get("organisationUnits").getAsJsonArray();
Assert.assertEquals(1, orgUnits.size());
JsonObject orgUnit = orgUnits.get(0).getAsJsonObject();
Assert.assertEquals(level, orgUnit.get("level").getAsInt());
Assert.assertEquals("MULTI_POLYGON", orgUnit.get("featureType").getAsString());
if (level == 0) {
Assert.assertEquals(AllAttributesDataset.GO_ALL.getCode(), orgUnit.get("code").getAsString());
} else {
Assert.assertEquals(go.getCode(), orgUnit.get("code").getAsString());
Assert.assertTrue(orgUnit.has("attributeValues"));
JsonArray attributeValues = orgUnit.get("attributeValues").getAsJsonArray();
Assert.assertEquals(1, attributeValues.size());
JsonObject attributeValue = attributeValues.get(0).getAsJsonObject();
Assert.assertNotNull(attributeValue.get("lastUpdated").getAsString());
Assert.assertNotNull(attributeValue.get("created").getAsString());
AttributeType attrDto = attr.fetchDTO();
if (attrDto instanceof AttributeIntegerType) {
Assert.assertEquals(go.getServerObject().getValue(attr.getAttributeName()), attributeValue.get("value").getAsLong());
} else if (attrDto instanceof AttributeFloatType) {
Assert.assertEquals(go.getServerObject().getValue(attr.getAttributeName()), attributeValue.get("value").getAsDouble());
} else if (attrDto instanceof AttributeDateType) {
// TODO : If we fetch the object from the database in this manner the
// miliseconds aren't included on the date. But if we fetch the object
// via a query (as in DataExportJob) then the miliseconds ARE
// included...
// String expected =
// DHIS2GeoObjectJsonAdapters.DHIS2Serializer.formatDate((Date)
// go.getServerObject().getValue(attr.getAttributeName()));
String expected = DHIS2GeoObjectJsonAdapters.DHIS2Serializer.formatDate(AllAttributesDataset.GO_DATE_VALUE);
String actual = attributeValue.get("value").getAsString();
Assert.assertEquals(expected, actual);
} else if (attrDto instanceof AttributeBooleanType) {
Assert.assertEquals(go.getServerObject().getValue(attr.getAttributeName()), attributeValue.get("value").getAsBoolean());
} else if (attrDto instanceof AttributeTermType) {
String dhis2Id = attributeValue.get("value").getAsString();
// Term term = (Term)
// go.getServerObject().getValue(attr.getAttributeName());
Assert.assertEquals("TEST_EXTERNAL_ID", dhis2Id);
} else {
Assert.assertEquals(go.getServerObject().getValue(attr.getAttributeName()), attributeValue.get("value").getAsString());
}
Assert.assertEquals("TEST_EXTERNAL_ID", attributeValue.get("attribute").getAsJsonObject().get("id").getAsString());
}
}
}
Aggregations