summaryrefslogtreecommitdiffstats
path: root/sandbox/kgoodson/mappingFramework/sdo-snapshot/src/main/java/com/agfa/hap/sdo/DataAccessService.java
blob: 26f86cb1e91e0c68e5b5663ababa8ffb693e93e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.agfa.hap.sdo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.agfa.hap.sdo.implementation.SnapshotImplementation;
import com.agfa.hap.sdo.mapper.PartialDataObjectMapper;

public class DataAccessService {
	private final static PartialDataObjectMapper MAPPER = new PartialDataObjectMapper();
    public static Snapshot createSnapShot(SnapshotDefinition def, PartialDataObject root) {
    	return createSnapShot(MAPPER, def, root);
    }
    
    public static <T> Snapshot createSnapShot(DataMapper<T> mapper, SnapshotDefinition def, T root) {
    	List<T> roots = new ArrayList<T>(1);
    	roots.add(root);
        return new SnapshotImplementation(mapper, def, roots);
    }

    @SuppressWarnings("unchecked")
	public static <T> Snapshot createMultiSnapShot(SnapshotDefinition def, Collection<? extends T> root) {
        return createMultiSnapShot((DataMapper<T>) MAPPER, def, root);
    }

    public static <T> Snapshot createMultiSnapShot(DataMapper<T> mapper, SnapshotDefinition def, Collection<? extends T> root) {
        return new SnapshotImplementation(mapper, def, root);
    }

	public static PartialDataObject getRootObject(Snapshot s) {
		return getRootObject(MAPPER, s);
	}

	/**
	 * @return The first and only root object of the snapshot. This is always a different
	 * instance than the object that was used to create the snapshot.
	 * @throws RuntimeException if there are no root objects or if there is more than one root object 
	 */
	public static <T> T getRootObject(DataMapper<T> mapper, Snapshot s) {
		List<T> roots = getRootObjects(mapper, s);
		if (roots.isEmpty()){
			throw new RuntimeException("snapshot is empty");
		}
		if (roots.size() > 1){
			throw new RuntimeException("more than 1 rootobject:" + roots.size());
		}
		return roots.get(0);
	}
	
	public static List<PartialDataObject> getRootObjects(Snapshot s) {
		return getRootObjects(MAPPER, s);
	}

	public static <T> List<T> getRootObjects(DataMapper<T> mapper, Snapshot s) {
		return s.extract(mapper);		
	}
	
	public static <T> Iterator<T> rootObjectsIterator(DataMapper<T> mapper, Snapshot s) {
		return getRootObjects(mapper, s).iterator();		
	}

}