The triggers aren't complicated, just seems a complicated solution for entering a simple attribute.
Anyway lets not argue about that because I have created the trigger, but I've found some very strange behaviour when I try to set the card attributes using the manually entered attributes.
First it all works fine if I use psql or pgadmin to INSERT or UPDATE the card's attribute. However when I try to use the CMDBuild GUI to update the Card the corresponding record was not created in the gis 'Detail' table. At first the error appeared to be trigger wasn't being fired. The only way I could get it to work was by first clicking the 'Add' button on the little pop up window that appears on the map.
I then set about tracing the flow through the numerous triggers in CMDB and it looks like what is happening is when I use the GUI is:
- My trigger is fired and it inserts the record. This is as expected.
- Then afterwards _cm_trigger_sanity_check_simple is triggered for some reason by a DELETE OP to delete the gis table records associated to the card. Note - this does NOT happen if do the insert or update from pg_admin.
So all I can think is that somewhere in CMDB there is some logic which says if you haven't clicked the 'Add' button then to automatically delete any gis 'Detail' records related to the card.
So I'm wondering if there is something else I need to do in the trigger to stop this happening?
Below is the logging, see the last 2 statements for the unexplained DELETE operations. The Card name is AccessPoint, the attribute I added for the entering the GPS coordinates is gpsCoordinates and the Card id I was updating was 79.
<code>
2014-12-10 11:14:38 GMT LOG: ===AFTER INSERT=====> Inserting record access point 79, POINT(4349478.70649849 -31660.9516805029), newRecId 359
2014-12-10 11:14:38 GMT CONTEXT: SQL statement "SELECT ap_updateGisRecordFromGPSCoordStr(NEW."Id", NEW."gpsCoordinates")"
PL/pgSQL function _update_access_point_gis_from_gps_location() line 18 at PERFORM
2014-12-10 11:14:38 GMT STATEMENT: UPDATE "AccessPoint" SET "Notes" = $1, "Description" = $2, "SSID" = $3, "MaxSpeedMBPS" = $4, "Contact" = $5, "Hotspot" = $6, "DeviceType" = $7, "Code" = $8, "gpsCoordinates" = $9, "User" = $10 WHERE "Id" = $11
2014-12-10 11:14:38 GMT LOG: *** AFTER UPDATE. Id 79, new gps S0.284414 , E39.072032, old gps S0.284414 , E39.072032
2014-12-10 11:14:38 GMT STATEMENT: UPDATE "AccessPoint" SET "Notes" = $1, "Description" = $2, "SSID" = $3, "MaxSpeedMBPS" = $4, "Contact" = $5, "Hotspot" = $6, "DeviceType" = $7, "Code" = $8, "gpsCoordinates" = $9, "User" = $10 WHERE "Id" = $11
2014-12-10 11:14:38 GMT LOG: ===_cm_trigger_sanity_check_simple====> DELETE OP, return OLD
2014-12-10 11:14:38 GMT STATEMENT: DELETE FROM "gis"."Detail_AccessPoint_gpsLocation" WHERE "Master" = 79
</code>
Finally here is logging for updating the same card using pgadmin, notice there is not delete operation.
<code>
014-12-10 11:42:59 GMT LOG: ===_cm_trigger_sanity_check_simple====> MUST BE AN INSERT 79
2014-12-10 11:42:59 GMT CONTEXT: SQL statement "INSERT INTO gis."Detail_AccessPoint_gpsLocation" ("Master","Geometry")
VALUES(accessPointId, apPoint)
RETURNING "Id""
PL/pgSQL function ap_updategisrecordfromgpscoordstr(bigint,character varying) line 46 at SQL statement
SQL statement "SELECT ap_updateGisRecordFromGPSCoordStr(NEW."Id", NEW."gpsCoordinates")"
PL/pgSQL function _update_access_point_gis_from_gps_location() line 18 at PERFORM
2014-12-10 11:42:59 GMT STATEMENT: UPDATE "AccessPoint" SET "gpsCoordinates"='S1.28356533 ,E39.07370596' WHERE "Id" = 79;
2014-12-10 11:42:59 GMT LOG: ===AFTER INSERT=====> Inserting record access point 79, POINT(4349665.0508733 -142897.792106271), newRecId 366
2014-12-10 11:42:59 GMT CONTEXT: SQL statement "SELECT ap_updateGisRecordFromGPSCoordStr(NEW."Id", NEW."gpsCoordinates")"
PL/pgSQL function _update_access_point_gis_from_gps_location() line 18 at PERFORM
2014-12-10 11:42:59 GMT STATEMENT: UPDATE "AccessPoint" SET "gpsCoordinates"='S1.28356533 ,E39.07370596' WHERE "Id" = 79;
2014-12-10 11:42:59 GMT LOG: *** AFTER UPDATE. Id 79, new gps S1.28356533 ,E39.07370596, old gps S1.28356533 ,E39.07370596
2014-12-10 11:42:59 GMT STATEMENT: UPDATE "AccessPoint" SET "gpsCoordinates"='S1.28356533 ,E39.07370596' WHERE "Id" = 79;
</code>
Previously Tecnoteca wrote:
Previously David wrote:
This does seem rather complicated for a simple data entry, also it appears to me we would to duplicate this trigger for every Card that has a GPS location.
In addition would this method continue to work if someone chose to change the location using standard method of selecting a point on the map?
Obviously yes.
CMDBuild Team