/** * * 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 Snapshot createSnapShot(DataMapper mapper, SnapshotDefinition def, T root) { List roots = new ArrayList(1); roots.add(root); return new SnapshotImplementation(mapper, def, roots); } @SuppressWarnings("unchecked") public static Snapshot createMultiSnapShot(SnapshotDefinition def, Collection root) { return createMultiSnapShot((DataMapper) MAPPER, def, root); } public static Snapshot createMultiSnapShot(DataMapper mapper, SnapshotDefinition def, Collection 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 getRootObject(DataMapper mapper, Snapshot s) { List 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 getRootObjects(Snapshot s) { return getRootObjects(MAPPER, s); } public static List getRootObjects(DataMapper mapper, Snapshot s) { return s.extract(mapper); } public static Iterator rootObjectsIterator(DataMapper mapper, Snapshot s) { return getRootObjects(mapper, s).iterator(); } }