/*
 * Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of fiware-iotagent-lib
 *
 * fiware-iotagent-lib is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * fiware-iotagent-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with fiware-iotagent-lib.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::daniel.moranjimenez@telefonica.com
 */

'use strict';

function fromBasicToExtended(date) {
    var split = date.match(/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})/);

    if (split) {
        return '+00' + split[1] + '-' + split[2] + '-' + split[3] + 'T' +
            split[4] + ':' + split[5] + ':' + split[6];
    } else {
        return null;
    }
}

function fromExtendedToBasic(date) {
    var split = date.match(/\+00(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/);

    if (split) {
        return split[1] + split[2] + split[3] + 'T' + split[4] + split[5] + split[6];
    } else {
        return null;
    }
}

function processAttributeUpdate(attribute) {
    if (attribute.type && attribute.type.toLowerCase && attribute.type.toLowerCase() === 'timestamp') {
        attribute.value = fromBasicToExtended(attribute.value);
    }

    return attribute;
}

function processAttributeQuery(attribute) {
    if (attribute.type && attribute.type.toLowerCase && attribute.type.toLowerCase() === 'timestamp') {
        attribute.value = fromExtendedToBasic(attribute.value);
    }

    return attribute;
}

function processEntityUpdate(entity) {
    entity.attributes = entity.attributes.map(processAttributeUpdate);
    return entity;
}

function processEntityQuery(entity) {
    entity.contextElement.attributes = entity.contextElement.attributes.map(processAttributeQuery);
    return entity;
}

function update(entity, callback) {
    entity.contextElements = entity.contextElements.map(processEntityUpdate);

    callback(null, entity);
}

function query(entity, callback) {
    entity.contextResponses = entity.contextResponses.map(processEntityQuery);

    callback(null, entity);
}

exports.update = update;
exports.query = query;

@@ -1533,274 +1533,56 @@
 ion 
-processAttributeUpdate(attribute) %7B%0A    if (attribute.type && attribute.type.toLowerCase && attribute.type.toLowerCase() === 'timestamp') %7B%0A        attribute.value = fromBasicToExtended(attribute.value);%0A    %7D%0A%0A    return attribute;%0A%7D%0A%0Afunction processAttributeQuery
+createProcessAttribute(fn) %7B%0A    return function
 (att
@@ -1587,24 +1587,28 @@
 ttribute) %7B%0A
+    
     if (attr
@@ -1707,16 +1707,20 @@
         
+    
 attribut
@@ -1730,34 +1730,17 @@
 alue = f
-romExtendedToBasic
+n
 (attribu
@@ -1754,19 +1754,27 @@
 e);%0A    
+ 
-%7D%0A%0A
+   %7D%0A%0A    
     retu
@@ -1787,16 +1787,23 @@
 ribute;%0A
+    %7D;%0A
 %7D%0A%0Afunct
@@ -1874,33 +1874,39 @@
 .attributes.map(
-p
+createP
 rocessAttributeU
@@ -1904,22 +1904,37 @@
 ttribute
-Update
+(fromBasicToExtended)
 );%0A    r
@@ -2068,29 +2068,60 @@
 map(
-processAttributeQuery
+%0A        createProcessAttribute(fromExtendedToBasic)
 );%0A 

REFACTOR some of the plugin functions