/*
 * Copyright 2010-2016 OrientDB LTD (http://orientdb.com)
 * Copyright 2013 Geomatys.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.core.sql.method.misc;

import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.util.ODateHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * 
 * @author Johann Sorel (Geomatys)
 * @author Luca Garulli (l.garulli--(at)--orientdb.com)
 */
public class OSQLMethodFormat extends OAbstractSQLMethod {

  public static final String NAME = "format";

  public OSQLMethodFormat() {
    super(NAME, 1, 2);
  }

  @Override
  public Object execute(final Object iThis, final OIdentifiable iRecord, final OCommandContext iContext, Object ioResult,
      final Object[] iParams) {

    // TRY TO RESOLVE AS DYNAMIC VALUE
    Object v = getParameterValue(iRecord, iParams[0].toString());
    if (v == null)
      // USE STATIC ONE
      v = iParams[0].toString();

    if (v != null) {
      if (ioResult instanceof Date) {
        final SimpleDateFormat format = new SimpleDateFormat(v.toString());
        if (iParams.length > 1) {
          format.setTimeZone(TimeZone.getTimeZone(iParams[1].toString()));
        } else {
          format.setTimeZone(ODateHelper.getDatabaseTimeZone());
        }
        ioResult = format.format(ioResult);
      } else {
        ioResult = ioResult != null ? String.format(v.toString(), ioResult) : null;
      }
    }

    return ioResult;
  }
}

@@ -705,16 +705,76 @@
 .misc;%0A%0A
+import com.orientechnologies.common.collection.OMultiValue;%0A
 import c
@@ -831,16 +831,16 @@
 ontext;%0A
-
 import c
@@ -1013,50 +1013,16 @@
 til.
-Date;%0Aimport java.util.TimeZone
+*
 ;%0A%0A/**%0A
- * %0A
  * @
@@ -1656,16 +1656,607 @@
      if 
+(isCollectionOfDates(ioResult)) %7B%0A        List%3CString%3E result = new ArrayList%3CString%3E();%0A        Iterator%3CObject%3E iterator = OMultiValue.getMultiValueIterator(ioResult);%0A        final SimpleDateFormat format = new SimpleDateFormat(v.toString());%0A        if (iParams.length %3E 1) %7B%0A          format.setTimeZone(TimeZone.getTimeZone(iParams%5B1%5D.toString()));%0A        %7D else %7B%0A          format.setTimeZone(ODateHelper.getDatabaseTimeZone());%0A        %7D%0A        while (iterator.hasNext()) %7B%0A          result.add(format.format(iterator.next()));%0A        %7D%0A        ioResult = result;%0A      %7D else if 
 (ioResul
@@ -2711,16 +2711,16 @@
 %0A    %7D%0A%0A
-
     retu
@@ -2735,11 +2735,412 @@
 ult;%0A  %7D
+%0A%0A  private boolean isCollectionOfDates(Object ioResult) %7B%0A    if (OMultiValue.isMultiValue(ioResult)) %7B%0A      Iterator%3CObject%3E iterator = OMultiValue.getMultiValueIterator(ioResult);%0A      while (iterator.hasNext()) %7B%0A        Object item = iterator.next();%0A        if (item != null && !(item instanceof Date)) %7B%0A          return false;%0A        %7D%0A      %7D%0A      return true;%0A    %7D%0A    return false;%0A  %7D
 %0A%7D%0A

Enhance format() method to support collections of dates