/*
 * Copyright (c) 2010-2016. Axon Framework
 * 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 org.axonframework.eventhandling.tokenstore.jpa;

import org.axonframework.eventsourcing.eventstore.TrackingToken;
import org.axonframework.serialization.*;

import javax.persistence.Basic;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.MappedSuperclass;

/**
 * @author Rene de Waele
 */
@MappedSuperclass
public abstract class AbstractTokenEntry<T> {
    @Id
    private String processName;
    @Id
    private int segment;
    @Basic(optional = false)
    @Lob
    private T token;
    @Basic(optional = false)
    private String tokenType;

    public AbstractTokenEntry(String process, int segment, TrackingToken token, Serializer serializer,
                              Class<T> contentType) {
        this.processName = process;
        this.segment = segment;
        SerializedObject<T> serializedToken = serializer.serialize(token, contentType);
        this.token = serializedToken.getData();
        this.tokenType = serializedToken.getType().getName();
    }

    protected AbstractTokenEntry() {
    }

    public String getProcessName() {
        return processName;
    }

    public int getSegment() {
        return segment;
    }

    @SuppressWarnings("unchecked")
    public SerializedObject<T> getToken() {
        return new SimpleSerializedObject<>(token, (Class<T>) token.getClass(), getTokenType());
    }

    protected SerializedType getTokenType() {
        return new SimpleSerializedType(tokenType, null);
    }
}

@@ -819,16 +819,50 @@
 nce.Id;%0A
+import javax.persistence.IdClass;%0A
 import j
@@ -925,16 +925,71 @@
 erclass;
+%0Aimport java.io.Serializable;%0Aimport java.util.Objects;
 %0A%0A/**%0A *
@@ -1033,16 +1033,54 @@
 erclass%0A
+@IdClass(AbstractTokenEntry.PK.class)%0A
 public a
@@ -2208,11 +2208,672 @@
 );%0A    %7D
+%0A%0A    @SuppressWarnings(%22UnusedDeclaration%22)%0A    public static class PK implements Serializable %7B%0A        private static final long serialVersionUID = 1L;%0A%0A        private String processName;%0A        private int segment;%0A%0A        @Override%0A        public boolean equals(Object o) %7B%0A            if (this == o) return true;%0A            if (o == null %7C%7C getClass() != o.getClass()) return false;%0A            PK pk = (PK) o;%0A            return segment == pk.segment &&%0A                    Objects.equals(processName, pk.processName);%0A        %7D%0A%0A        @Override%0A        public int hashCode() %7B%0A            return Objects.hash(processName, segment);%0A        %7D%0A    %7D
 %0A%7D%0A

introduce IdClass for composite PK