/*
 *
 *  *  Copyright 2010-2016 OrientDB LTD (http://orientdb.com)
 *  *
 *  *  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.
 *  *
 *  * For more information: http://orientdb.com
 *
 */
package com.orientechnologies.orient.server.distributed.impl.task;

import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.serialization.OStreamable;
import com.orientechnologies.orient.server.distributed.impl.task.transaction.*;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author tglman
 */
public class OTransactionPhase1TaskResult implements OStreamable {

  private OTransactionResultPayload resultPayload;

  public OTransactionPhase1TaskResult() {

  }

  public OTransactionPhase1TaskResult(OTransactionResultPayload resultPayload) {
    this.resultPayload = resultPayload;
  }

  @Override
  public void toStream(final DataOutput out) throws IOException {
    out.writeInt(resultPayload.getResponseType());
    switch (resultPayload.getResponseType()) {
    case OTxSuccess.ID:
    case OTxLockTimeout.ID:
      break;
    case OTxConcurrentModification.ID:
      OTxConcurrentModification pl = (OTxConcurrentModification) resultPayload;
      out.writeInt(pl.getRecordId().getClusterId());
      out.writeLong(pl.getRecordId().getClusterPosition());
      out.writeInt(pl.getVersion());
      break;
    case OTxException.ID:
      throw new UnsupportedOperationException(); //TODO!
    case OTxUniqueIndex.ID:
      throw new UnsupportedOperationException(); //TODO!
    }
  }

  @Override
  public void fromStream(final DataInput in) throws IOException {
    int type = in.readInt();
    switch (resultPayload.getResponseType()) {
    case OTxSuccess.ID:
      this.resultPayload = new OTxSuccess();
      break;
    case OTxLockTimeout.ID:
      this.resultPayload = new OTxLockTimeout();
      break;
    case OTxConcurrentModification.ID:
      ORecordId rid = new ORecordId(in.readInt(), in.readLong());
      int version = in.readInt();
      this.resultPayload = new OTxConcurrentModification(rid, version);
      break;
    case OTxException.ID:
      throw new UnsupportedOperationException(); //TODO!
    case OTxUniqueIndex.ID:
      throw new UnsupportedOperationException(); //TODO!
    }
  }

  public OTransactionResultPayload getResultPayload() {
    return resultPayload;
  }
}

@@ -894,33 +894,280 @@
 ore.
-serialization.OStreamable
+metadata.schema.OType;%0Aimport com.orientechnologies.orient.core.serialization.OStreamable;%0Aimport com.orientechnologies.orient.core.serialization.OStreamableHelper;%0Aimport com.orientechnologies.orient.core.serialization.serializer.record.binary.ORecordSerializerNetworkV37
 ;%0Aim
@@ -2206,38 +2206,102 @@
     
-throw new UnsupportedOperation
+OTxException pl2 = (OTxException) resultPayload;%0A      OStreamableHelper.toStream(out, pl2.get
 Exce
@@ -2303,33 +2303,26 @@
 tException()
+)
 ;
- //TODO!
 %0A    case OT
@@ -2348,58 +2348,643 @@
     
-throw new UnsupportedOperationException(); //TODO!
+OTxUniqueIndex pl3 = (OTxUniqueIndex) resultPayload;%0A      //RID%0A      out.writeInt(pl3.getRecordId().getClusterId());%0A      out.writeLong(pl3.getRecordId().getClusterPosition());%0A      //index name%0A      String indexName = pl3.getIndex();%0A      byte%5B%5D indexNameBytes = indexName.getBytes();%0A      out.writeInt(indexNameBytes.length);%0A      out.write(indexNameBytes);%0A      //index key%0A      OType type = OType.getTypeByValue(pl3.getKey());%0A      out.writeInt(type.getId());%0A      byte%5B%5D keyBytes = ORecordSerializerNetworkV37.INSTANCE.serializeValue(pl3.getKey(), type);%0A      out.writeInt(keyBytes.length);%0A      out.write(keyBytes);
 %0A   
@@ -3577,143 +3577,821 @@
     
-t
+T
 hrow
- new UnsupportedOperationException(); //TODO!%0A    case OTxUniqueIndex.ID:%0A      throw new UnsupportedOperationException(); //TODO!
+able exception = (Throwable) OStreamableHelper.fromStream(in);%0A      this.resultPayload = new OTxException(exception);%0A    case OTxUniqueIndex.ID:%0A      //RID%0A      ORecordId rid2 = new ORecordId(in.readInt(), in.readLong());%0A      //index name%0A      int indexNameSize = in.readInt();%0A      byte%5B%5D indexNameBytes = new byte%5BindexNameSize%5D;%0A      in.readFully(indexNameBytes);%0A      String indexName = new String(indexNameBytes);%0A      //index key%0A      int type2Id = in.readInt();%0A      OType type2 = OType.getById((byte) type2Id);%0A      int keySize = in.readInt();%0A      byte%5B%5D keyBytes = new byte%5BkeySize%5D;%0A      in.readFully(keyBytes);%0A      Object keyValue = ORecordSerializerNetworkV37.INSTANCE.deserializeValue(keyBytes, type2);%0A      this.resultPayload = new OTxUniqueIndex(rid2, indexName, keyValue);
 %0A   

Implement serialization of OTxException and OTxUniqueIndex