Advertisement
carlos18619

Untitled

Nov 7th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. *********************** GROOVY SCRIPT USED TO SYNC - SyncScript.groovy ***********************************************
  2. /*
  3. * Copyright (c) 2010-2013 Evolveum
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import groovy.sql.Sql;
  18. import groovy.sql.DataSet;
  19.  
  20. // Parameters:
  21. // The connector sends the following:
  22. // connection: handler to the SQL connection
  23. // objectClass: a String describing the Object class (__ACCOUNT__ / __GROUP__ / other)
  24. // action: a string describing the action ("SYNC" or "GET_LATEST_SYNC_TOKEN" here)
  25. // log: a handler to the Log facility
  26. // options: a handler to the OperationOptions Map (null if action = "GET_LATEST_SYNC_TOKEN")
  27. // token: a handler to an Object representing the sync token (null if action = "GET_LATEST_SYNC_TOKEN")
  28. //
  29. //
  30. // Returns:
  31. // if action = "GET_LATEST_SYNC_TOKEN", it must return an object representing the last known
  32. // sync token for the corresponding ObjectClass
  33. //
  34. // if action = "SYNC":
  35. // A list of Maps . Each map describing one update:
  36. // Map should look like the following:
  37. //
  38. // [
  39. // "token": <Object> token object (could be Integer, Date, String) , [!! could be null]
  40. // "operation":<String> ("CREATE_OR_UPDATE"|"DELETE") will always default to CREATE_OR_DELETE ,
  41. // "uid":<String> uid (uid of the entry) ,
  42. // "previousUid":<String> prevuid (This is for rename ops) ,
  43. // "password":<String> password (optional... allows to pass clear text password if needed),
  44. // "attributes":Map<String,List> of attributes name/values
  45. // ]
  46.  
  47. log.info("Entering "+action+" Script");
  48. def sql = new Sql(connection);
  49.  
  50. def now = new Date()
  51.  
  52. def file1 = new File('/var/opt/midpoint/icf-connectors/oracle/completo2/sync.log');
  53. if (file1.exists()) {
  54. file1.delete()
  55. }
  56. file1 << "Sync Test" << "\n"
  57. file1 << "Date = $now" << "\n"
  58. file1 << "objectClass = $objectClass" << "\n"
  59. file1 << "action = $action" << "\n"
  60. //file1 << "options = $options" << "\n"
  61.  
  62. if (action.equalsIgnoreCase("GET_LATEST_SYNC_TOKEN")) {
  63. // XXX the following line is probably fine for MySQL
  64. row = sql.firstRow("select timestamp from Users order by timestamp desc")
  65.  
  66. // the following line is for PostgreSQL with TIMESTAMP columns. We will
  67. // "truncate" the timestamp to milliseconds
  68. // row = sql.firstRow("select date_trunc('milliseconds', timestamp) as timestamp from Users order by timestamp desc;")
  69.  
  70. log.ok("Get Latest Sync Token script: last token is: "+row["timestamp"])
  71. // We don't wanna return the java.sql.Timestamp, it is not a supported data type
  72. // Get the 'long' version
  73. return row["timestamp"].getTime();
  74. }
  75.  
  76. else if (action.equalsIgnoreCase("SYNC")) {
  77. def result = []
  78. def tstamp = null
  79. if (token != null){
  80. tstamp = new java.sql.Timestamp(token)
  81. }
  82. else{
  83. def today= new Date()
  84. tstamp = new java.sql.Timestamp(today.time)
  85. }
  86.  
  87. // XXX the following line is probably fine for MySQL
  88. switch ( objectClass ) {
  89. case "__ACCOUNT__":
  90.  
  91. sql.eachRow("select * from Users where timestamp > ${tstamp}",
  92. {result.add([operation:"CREATE_OR_UPDATE", uid:it.id.toString(), token:it.timestamp.getTime(), attributes:[__NAME__:it.login, firstname:it.firstname, lastname:it.lastname, fullname:it.fullname, organization:it.organization, email:it.email, __ENABLE__:!(it.disabled as Boolean), codfun:it.codfun, codpre:it.codpre, codcrg:it.codcrg, cencus:it.cencus]])}
  93.  
  94. )
  95. case "Organization":
  96. sql.eachRow("select * from Organizations where timestamp > ${tstamp} ",
  97. {result.add([operation:"CREATE_OR_UPDATE", uid:it.id.toString(), token:it.timestamp.getTime(), attributes:[__NAME__:it.name, description:it.description, codemp:it.codemp, pai:it.pai]])}
  98. )
  99. }
  100.  
  101.  
  102. log.ok("Sync script: found "+result.size()+" events to sync")
  103. file1 << "result = $result" << "\n"
  104. return result;
  105. }
  106. else { // action not implemented
  107. log.error("Sync script: action '"+action+"' is not implemented in this script")
  108. return null;
  109. }
  110.  
  111.  
  112. ************************** OUTPUT OF SyncScript.groovy **********************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement