/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.cuba.gui.app.core.entitydiff;

import com.haulmont.cuba.core.app.EntitySnapshotService;
import com.haulmont.cuba.core.entity.BaseEntity;
import com.haulmont.cuba.core.entity.EntitySnapshot;
import com.haulmont.cuba.core.global.MetadataProvider;
import com.haulmont.cuba.gui.ServiceLocator;
import com.haulmont.cuba.gui.data.impl.CollectionDatasourceImpl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author artamonov
 * @version $Id$
 */
public class EntitySnapshotsDatasource extends CollectionDatasourceImpl<EntitySnapshot, UUID> {

    private BaseEntity entity;
    private List<EntitySnapshot> snapshots;

    @Override
    public boolean isModified() {
        return false;
    }

    public BaseEntity getEntity() {
        return entity;
    }

    public void setEntity(BaseEntity entity) {
        this.entity = entity;
    }

    @Override
    protected void loadData(Map<String, Object> params) {
        clear();

        if (entity != null) {
            EntitySnapshotService snapshotService = ServiceLocator.lookup(EntitySnapshotService.NAME);
            snapshots = snapshotService.getSnapshots(
                    MetadataProvider.getSession().getClass(entity.getClass()),
                    entity.getUuid());

            for (EntitySnapshot snapshot : snapshots) {
                data.put(snapshot.getId(), snapshot);
                attachListener(snapshot);
            }
        }
    }

    public EntitySnapshot getLatestSnapshot() {
        if ((snapshots != null) && (snapshots.size() > 0))
            return snapshots.get(snapshots.size() - 1);
        return null;
    }
}

@@ -411,70 +411,16 @@
 bal.
-MetadataProvider;%0D%0Aimport com.haulmont.cuba.gui.ServiceLocator
+AppBeans
 ;%0D%0Ai
@@ -714,21 +714,23 @@
 %0D%0A    pr
-ivate
+otected
  BaseEnt
@@ -752,13 +752,15 @@
   pr
-ivate
+otected
  Lis
@@ -1212,29 +1212,20 @@
 e = 
-ServiceLocator.lookup
+AppBeans.get
 (Ent
@@ -1308,69 +1308,8 @@
 ots(
-%0D%0A                    MetadataProvider.getSession().getClass(
 enti
@@ -1318,38 +1318,20 @@
 .get
+Meta
 Class()
-)
 ,
-%0D%0A                   
  ent
@@ -1734,11 +1734,9 @@
 %0A    %7D%0D%0A
+
 %7D
-%0D%0A

Refactor usage of deprecated API from EntitySnapshotsDatasource #PL-5309