LCOV - code coverage report
Current view: top level - lib/src - wilt_user_utils.dart (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 52 56 92.9 %
Date: 2017-07-19 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  * Packge : Wilt
       3             :  * Author : S. Hamblett <steve.hamblett@linux.com>
       4             :  * Date   : 04/06/2013
       5             :  * Copyright :  S.Hamblett@OSCF
       6             :  *
       7             :  * This class provides Wilt user utility functions 
       8             :  */
       9             : 
      10             : part of wilt;
      11             : 
      12             : class WiltUserUtils {
      13             :   /// Get a document id from a JSON Object
      14             :   static String getDocumentId(jsonobject.JsonObject response) {
      15           1 :     final String temp = JSON.encode(response);
      16           1 :     final Map tempMap = JSON.decode(temp);
      17           1 :     return tempMap["_id"];
      18             :   }
      19             : 
      20             :   /// Get a revision from a JSON object
      21             :   static String getDocumentRev(jsonobject.JsonObject response) {
      22           1 :     final String temp = JSON.encode(response);
      23           1 :     final Map tempMap = JSON.decode(temp);
      24           1 :     return tempMap["_rev"];
      25             :   }
      26             : 
      27             :   /// Adds a CouchDB _rev to the JSON body of a document
      28             :   static String addDocumentRev(
      29             :       jsonobject.JsonObject document, String revision) {
      30           1 :     final String temp = JSON.encode(document);
      31           1 :     final Map tempMap = JSON.decode(temp);
      32           1 :     tempMap["_rev"] = revision;
      33           1 :     return JSON.encode(tempMap);
      34             :   }
      35             : 
      36             :   /// Adds a CouchDB _id to the JSON body of a document
      37             :   static String addDocumentId(jsonobject.JsonObject document, String id) {
      38           1 :     final String temp = JSON.encode(document);
      39           1 :     final Map tempMap = JSON.decode(temp);
      40           1 :     tempMap["_id"] = id;
      41           1 :     return JSON.encode(tempMap);
      42             :   }
      43             : 
      44             :   /// Adds a CouchDB _rev to the JSON body of a document
      45             :   static jsonobject.JsonObject addDocumentRevJo(
      46             :       jsonobject.JsonObject document, String revision) {
      47           1 :     final String temp = JSON.encode(document);
      48           1 :     final Map tempMap = JSON.decode(temp);
      49           1 :     tempMap["_rev"] = revision;
      50           1 :     return new jsonobject.JsonObject.fromMap(tempMap);
      51             :   }
      52             : 
      53             :   /// Adds a CouchDB _id to the JSON body of a document
      54             :   static jsonobject.JsonObject addDocumentIdJo(
      55             :       jsonobject.JsonObject document, String id) {
      56           1 :     final String temp = JSON.encode(document);
      57           1 :     final Map tempMap = JSON.decode(temp);
      58           1 :     tempMap["_id"] = id;
      59           1 :     return new jsonobject.JsonObject.fromMap(tempMap);
      60             :   }
      61             : 
      62             :   /// Adds a CouchDB _deleted to the JSON body of a document
      63             :   static String addDocumentDeleted(jsonobject.JsonObject document) {
      64           0 :     final String temp = JSON.encode(document);
      65           0 :     final Map tempMap = JSON.decode(temp);
      66           0 :     tempMap["_deleted"] = true;
      67           0 :     return JSON.encode(tempMap);
      68             :   }
      69             : 
      70             :   /// Adds a CouchDB _deleted to the JSON body of a document
      71             :   static jsonobject.JsonObject addDocumentDeleteJo(
      72             :       jsonobject.JsonObject document) {
      73           1 :     final String temp = JSON.encode(document);
      74           1 :     final Map tempMap = JSON.decode(temp);
      75           1 :     tempMap["_deleted"] = true;
      76           1 :     return new jsonobject.JsonObject.fromMap(tempMap);
      77             :   }
      78             : 
      79             :   /// Adds both a CouchDb _id and _rev to the JSON body of a document
      80             :   static jsonobject.JsonObject addDocumentIdRevJojsonobject(
      81             :       jsonobject.JsonObject document, String id, String rev) {
      82           1 :     final String temp = JSON.encode(document);
      83           1 :     final Map tempMap = JSON.decode(temp);
      84           1 :     tempMap["_id"] = id;
      85           1 :     tempMap['_rev'] = rev;
      86           1 :     return new jsonobject.JsonObject.fromMap(tempMap);
      87             :   }
      88             : 
      89             :   /// Creates a JSON string for bulk inserts where an
      90             :   /// _id and or _rev is needed form document strings
      91             :   static String createBulkInsertString(List<String> docStrings) {
      92             :     String innerString = " ";
      93           2 :     for (String doc in docStrings) {
      94           1 :       innerString = "$innerString$doc,";
      95             :     }
      96             : 
      97             :     /* Remove the last ',' */
      98           1 :     final int len = innerString.length;
      99           2 :     innerString = innerString.substring(0, len - 1);
     100           1 :     final String insertString = '{"docs":[$innerString]}';
     101           1 :     return insertString.trim();
     102             :   }
     103             : 
     104             :   /// Creates a JSON string for bulk inserts where an
     105             :   /// _id and or _rev is needed from JsonObjects.
     106             :   static String createBulkInsertStringJo(List<jsonobject.JsonObject> records) {
     107           1 :     final List<String> docStrings = new List<String>();
     108           1 :     records.forEach((record) {
     109           2 :       docStrings.add(record.toString());
     110             :     });
     111             : 
     112           1 :     return createBulkInsertString(docStrings);
     113             :   }
     114             : 
     115             :   /// Get a list of attachments from a document.
     116             :   ///
     117             :   /// Returned Json Object contains the fields 'name' and 'data', the data
     118             :   /// being the attachment data returned from CouchDb.
     119             :   static List<jsonobject.JsonObject> getAttachments(
     120             :       jsonobject.JsonObject document) {
     121           1 :     final List attachmentsList = new List<jsonobject.JsonObject>();
     122           1 :     final String docString = document.toString();
     123           1 :     final Map docMap = JSON.decode(docString);
     124           1 :     if (docMap.containsKey('_attachments')) {
     125           1 :       final Map attachmentList = docMap['_attachments'];
     126           2 :       attachmentList.keys.forEach((key) {
     127             :         final jsonobject.JsonObject jsonAttachmentData =
     128           2 :             new jsonobject.JsonObject.fromMap(attachmentList[key]);
     129             :         final jsonobject.JsonObject jsonAttachment =
     130           1 :             new jsonobject.JsonObject();
     131           1 :         jsonAttachment.name = key;
     132           1 :         jsonAttachment.data = jsonAttachmentData;
     133           1 :         attachmentsList.add(jsonAttachment);
     134             :       });
     135             :     }
     136             : 
     137             :     return attachmentsList;
     138             :   }
     139             : }

Generated by: LCOV version 1.10