Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new feature: userid for PGString #49

Merged
merged 1 commit into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ public class PersistenceSettings {
private static final String TAG_IMPLEMENTATION_CLASS = "persistenceManagerImplementationClass";
private static final String DEFAULT_IMPLEMENTATION_CLASS = "de.fraunhofer.iosb.ilt.sta.persistence.postgres.longid.PostgresPersistenceManagerLong";
private static final String TAG_ALWAYS_ORDERBY_ID = "alwaysOrderbyId";
private static final String TAG_ID_GENERATION_MODE = "idGenerationMode";

private static final List<String> ALL_PROPERTIES = Arrays.asList(
TAG_IMPLEMENTATION_CLASS,
TAG_ALWAYS_ORDERBY_ID
TAG_ALWAYS_ORDERBY_ID,
TAG_ID_GENERATION_MODE
);

/**
* Fully-qualified class name of the PersistenceManager implementation class
*/
private String persistenceManagerImplementationClass;
private boolean alwaysOrderbyId = true;
private String idGenerationMode = "ServerGeneratedOnly";
/**
* Extension point for implementation specific settings
*/
Expand All @@ -58,6 +61,7 @@ public PersistenceSettings(Settings settings) {
private void init(Settings settings) {
persistenceManagerImplementationClass = settings.get(TAG_IMPLEMENTATION_CLASS, DEFAULT_IMPLEMENTATION_CLASS);
alwaysOrderbyId = settings.getBoolean(TAG_ALWAYS_ORDERBY_ID, alwaysOrderbyId);
idGenerationMode = settings.get(TAG_ID_GENERATION_MODE, idGenerationMode);
customSettings = settings;
}

Expand All @@ -72,4 +76,8 @@ public boolean getAlwaysOrderbyId() {
public Settings getCustomSettings() {
return customSettings;
}

public String getIdGenerationMode() {
return idGenerationMode;
}
}
6 changes: 6 additions & 0 deletions FROST-Server.HTTP/src/main/webapp/META-INF/context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
de.fraunhofer.iosb.ilt.sta.persistence.postgres.stringid.PostgresPersistenceManagerString
de.fraunhofer.iosb.ilt.sta.persistence.postgres.uuidid.PostgresPersistenceManagerUuid
-->
<Parameter override="false" name="persistence.idGenerationMode" value="ServerGeneratedOnly" description="Mode for id generation when using PostgresPersistenceManagerString."/>
<!-- All options:
"ServerGeneratedOnly" = No client defined ids allowed.
"ServerAndClientGenerated" = Both, server and client generated ids, are allowed.
"ClientGeneratedOnly" = Client has to provide @iot.id to create entities.
-->
<Parameter override="false" name="persistence.alwaysOrderbyId" value="false" description="Always add an 'orderby=id asc' to queries to ensure consistent paging."/>
<!-- JNDO Database connection. Does suppport connection pooling. -->
<Parameter override="false" name="persistence.db_jndi_datasource" value="jdbc/sensorThings" description="JNDI data source name"/>
Expand Down
6 changes: 6 additions & 0 deletions FROST-Server.MQTTP/src/main/webapp/META-INF/context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
de.fraunhofer.iosb.ilt.sta.persistence.postgres.stringid.PostgresPersistenceManagerString
de.fraunhofer.iosb.ilt.sta.persistence.postgres.uuidid.PostgresPersistenceManagerUuid
-->
<Parameter override="false" name="persistence.idGenerationMode" value="ServerGeneratedOnly" description="Mode for id generation when using PostgresPersistenceManagerString."/>
<!-- All options:
"ServerGeneratedOnly" = No client defined ids allowed.
"ServerAndClientGenerated" = Both, server and client generated ids, are allowed.
"ClientGeneratedOnly" = Client has to provide @iot.id to create entities.
-->
<Parameter override="false" name="persistence.alwaysOrderbyId" value="false" description="Always add an 'orderby=id asc' to queries to ensure consistent paging."/>
<!-- JNDO Database connection. Does suppport connection pooling. -->
<Parameter override="false" name="persistence.db_jndi_datasource" value="jdbc/sensorThings" description="JNDI data source name"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public boolean insertDatastream(Datastream ds) throws NoSuchEntityException, Inc
insert.set(qd.obsPropertyId, (String) op.getId().getValue());
insert.set(qd.sensorId, (String) s.getId().getValue());
insert.set(qd.thingId, (String) t.getId().getValue());

IdGenerationHandler idhandler = new IdGenerationHandler(ds);
if (idhandler.useClientSuppliedId()) {
idhandler.modifyClientSuppliedId();
insert.set(qd.id, (String) idhandler.getIdValue());
}

String datastreamId = insert.executeWithKey(qd.id);
LOGGER.debug("Inserted datastream. Created id = {}.", datastreamId);
Expand Down Expand Up @@ -259,6 +265,12 @@ public boolean insertMultiDatastream(MultiDatastream ds) throws NoSuchEntityExce

insert.set(qd.sensorId, (String) s.getId().getValue());
insert.set(qd.thingId, (String) t.getId().getValue());

IdGenerationHandler idhandler = new IdGenerationHandler(ds);
if (idhandler.useClientSuppliedId()) {
idhandler.modifyClientSuppliedId();
insert.set(qd.id, (String) idhandler.getIdValue());
}

String multiDatastreamId = insert.executeWithKey(qd.id);
LOGGER.debug("Inserted multiDatastream. Created id = {}.", multiDatastreamId);
Expand Down Expand Up @@ -427,6 +439,12 @@ public boolean insertFeatureOfInterest(FeatureOfInterest foi) throws NoSuchEntit
String encodingType = foi.getEncodingType();
insert.set(qfoi.encodingType, encodingType);
insertGeometry(insert, qfoi.feature, qfoi.geom, encodingType, foi.getFeature());

IdGenerationHandler idhandler = new IdGenerationHandler(foi);
if (idhandler.useClientSuppliedId()) {
idhandler.modifyClientSuppliedId();
insert.set(qfoi.id, (String) idhandler.getIdValue());
}

String generatedId = insert.executeWithKey(qfoi.id);
LOGGER.debug("Inserted FeatureOfInterest. Created id = {}.", generatedId);
Expand Down Expand Up @@ -587,6 +605,12 @@ public boolean insertHistoricalLocation(HistoricalLocation h) throws NoSuchEntit
SQLInsertClause insert = qFactory.insert(qhl);
insert.set(qhl.time, new Timestamp(h.getTime().getDateTime().getMillis()));
insert.set(qhl.thingId, (String) h.getThing().getId().getValue());

IdGenerationHandler idhandler = new IdGenerationHandler(h);
if (idhandler.useClientSuppliedId()) {
idhandler.modifyClientSuppliedId();
insert.set(qhl.id, (String) idhandler.getIdValue());
}

String generatedId = insert.executeWithKey(qhl.id);
LOGGER.debug("Inserted HistoricalLocation. Created id = {}.", generatedId);
Expand Down Expand Up @@ -665,6 +689,12 @@ public boolean insertLocation(Location l) throws NoSuchEntityException, Incomple
String encodingType = l.getEncodingType();
insert.set(ql.encodingType, encodingType);
insertGeometry(insert, ql.location, ql.geom, encodingType, l.getLocation());

IdGenerationHandler idhandler = new IdGenerationHandler(l);
if (idhandler.useClientSuppliedId()) {
idhandler.modifyClientSuppliedId();
insert.set(ql.id, (String) idhandler.getIdValue());
}

String locationId = insert.executeWithKey(ql.id);
LOGGER.debug("Inserted Location. Created id = {}.", locationId);
Expand Down Expand Up @@ -693,6 +723,7 @@ public boolean insertLocation(Location l) throws NoSuchEntityException, Incomple
insert = qFactory.insert(qhl);
insert.set(qhl.thingId, thingId);
insert.set(qhl.time, new Timestamp(Calendar.getInstance().getTimeInMillis()));
// TODO: maybe use histLocationId based on locationId
String histLocationId = insert.executeWithKey(qhl.id);
LOGGER.debug("Created historicalLocation {}", histLocationId);

Expand Down Expand Up @@ -810,6 +841,7 @@ public EntityChangedMessage updateLocation(Location l, String locationId) throws
insert = qFactory.insert(qhl);
insert.set(qhl.thingId, thingId);
insert.set(qhl.time, new Timestamp(Calendar.getInstance().getTimeInMillis()));
// TODO: maybe use histLocationId based on locationId
String histLocationId = insert.executeWithKey(qhl.id);
LOGGER.debug("Created historicalLocation {}", histLocationId);

Expand Down Expand Up @@ -900,6 +932,12 @@ public boolean insertObservation(Observation o) throws NoSuchEntityException, In
insert.set(qo.multiDatastreamId, (String) mds.getId().getValue());
}
insert.set(qo.featureId, (String) f.getId().getValue());

IdGenerationHandler idhandler = new IdGenerationHandler(o);
if (idhandler.useClientSuppliedId()) {
idhandler.modifyClientSuppliedId();
insert.set(qo.id, (String) idhandler.getIdValue());
}

String generatedId = insert.executeWithKey(qo.id);
LOGGER.debug("Inserted Observation. Created id = {}.", generatedId);
Expand Down Expand Up @@ -1046,6 +1084,12 @@ public boolean insertObservedProperty(ObservedProperty op) throws NoSuchEntityEx
insert.set(qop.name, op.getName());
insert.set(qop.description, op.getDescription());
insert.set(qop.properties, objectToJson(op.getProperties()));

IdGenerationHandler idhandler = new IdGenerationHandler(op);
if (idhandler.useClientSuppliedId()) {
idhandler.modifyClientSuppliedId();
insert.set(qop.id, (String) idhandler.getIdValue());
}

String generatedId = insert.executeWithKey(qop.id);
LOGGER.debug("Inserted ObservedProperty. Created id = {}.", generatedId);
Expand Down Expand Up @@ -1144,6 +1188,12 @@ public boolean insertSensor(Sensor s) throws NoSuchEntityException, IncompleteEn
// TODO: Check metadata serialisation.
insert.set(qs.metadata, s.getMetadata().toString());
insert.set(qs.properties, objectToJson(s.getProperties()));

IdGenerationHandler idhandler = new IdGenerationHandler(s);
if (idhandler.useClientSuppliedId()) {
idhandler.modifyClientSuppliedId();
insert.set(qs.id, (String) idhandler.getIdValue());
}

String generatedId = insert.executeWithKey(qs.id);
LOGGER.debug("Inserted Sensor. Created id = {}.", generatedId);
Expand Down Expand Up @@ -1259,6 +1309,12 @@ public boolean insertThing(Thing t) throws NoSuchEntityException, IncompleteEnti
insert.set(qt.name, t.getName());
insert.set(qt.description, t.getDescription());
insert.set(qt.properties, objectToJson(t.getProperties()));

IdGenerationHandler idhandler = new IdGenerationHandler(t);
if (idhandler.useClientSuppliedId()) {
idhandler.modifyClientSuppliedId();
insert.set(qt.id, (String) idhandler.getIdValue());
}

String thingId = insert.executeWithKey(qt.id);
LOGGER.debug("Inserted Thing. Created id = {}.", thingId);
Expand All @@ -1285,6 +1341,7 @@ public boolean insertThing(Thing t) throws NoSuchEntityException, IncompleteEnti
insert = qFactory.insert(qhl);
insert.set(qhl.thingId, thingId);
insert.set(qhl.time, new Timestamp(Calendar.getInstance().getTimeInMillis()));
// TODO: maybe use histLocationId based on locationIds
String histLocationId = insert.executeWithKey(qhl.id);
LOGGER.debug("Created historicalLocation {}", histLocationId);

Expand Down Expand Up @@ -1414,6 +1471,7 @@ public EntityChangedMessage updateThing(Thing t, String thingId) throws NoSuchEn
SQLInsertClause insert = qFactory.insert(qhl);
insert.set(qhl.thingId, thingId);
insert.set(qhl.time, new Timestamp(Calendar.getInstance().getTimeInMillis()));
// TODO: maybe use histLocationId based on locationIds
String histLocationId = insert.executeWithKey(qhl.id);
LOGGER.debug("Created historicalLocation {}", histLocationId);

Expand Down Expand Up @@ -1539,17 +1597,35 @@ private Object reParseGeometry(String encodingType, Object object) {
* complete and can thus not be created.
*/
private void entityExistsOrCreate(Entity e) throws NoSuchEntityException, IncompleteEntityException {
if (e != null && e.getId() == null) {
if (e == null) {
throw new NoSuchEntityException("No entity!");
}

if (e.getId() == null) {
e.complete();
// no id but complete -> create
pm.insert(e);
} else if (e == null || !entityExists(e)) {
if (e == null) {
throw new NoSuchEntityException("No entity!");
}
return;
}

if (entityExists(e)) {
return;
}

// check if this is an incomplete entity
try {
e.complete();
}
catch (IncompleteEntityException exc) {
// not complete and link entity does not exist
throw new NoSuchEntityException("No such entity '" + e.getEntityType() + "' with id " + e.getId().getValue());
}
}

// complete with id -> create
pm.insert(e);
return;
}

public boolean entityExists(Entity e) {
if (e == null || e.getId() == null) {
return false;
Expand Down
Loading