aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/java/org/eclipse/winery/repository/resources/_support/collections/withid/EntityWithIdCollectionResource.java
blob: f428752f6d3b3a3a59bcf61e32a8e1da9dc3c555 (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
/*******************************************************************************
 * Copyright (c) 2012-2013 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Oliver Kopp - initial API and implementation
 *******************************************************************************/
package org.eclipse.winery.repository.resources._support.collections.withid;

import java.lang.reflect.Constructor;
import java.util.List;

import org.eclipse.winery.repository.resources._support.IPersistable;
import org.eclipse.winery.repository.resources._support.collections.EntityCollectionResource;
import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class EntityWithIdCollectionResource<EntityResourceT extends EntityWithIdResource<EntityT>, EntityT> extends EntityCollectionResource<EntityResourceT, EntityT> {
	
	private static final Logger logger = LoggerFactory.getLogger(EntityWithIdCollectionResource.class);
	
	
	/**
	 * {@inheritDoc}
	 */
	public EntityWithIdCollectionResource(Class<EntityResourceT> entityResourceTClazz, Class<EntityT> entityTClazz, List<EntityT> list, IPersistable res) {
		super(entityResourceTClazz, entityTClazz, list, res);
	}
	
	/**
	 * Each CollectionResource has to implement the id getting by itself as
	 * TOSCA XSD does not provide a general purpose id fetching mechanism
	 */
	@Override
	public abstract String getId(EntityT entity);
	
	@Override
	protected EntityResourceT getEntityResourceInstance(EntityT entity, int idx) {
		Constructor<EntityResourceT> constructor;
		try {
			constructor = this.entityResourceTClazz.getConstructor(IIdDetermination.class, this.entityTClazz, int.class, List.class, this.res.getClass());
		} catch (Exception e) {
			try {
				constructor = this.entityResourceTClazz.getConstructor(IIdDetermination.class, this.entityTClazz, int.class, List.class, IPersistable.class);
			} catch (Exception e2) {
				EntityWithIdCollectionResource.logger.debug("Could not get constructor", e);
				EntityWithIdCollectionResource.logger.debug("res.getClass() was {}", this.res.getClass());
				throw new IllegalStateException(e2);
			}
		}
		EntityResourceT newInstance;
		try {
			newInstance = constructor.newInstance(this, entity, idx, this.list, this.res);
		} catch (Exception e) {
			EntityWithIdCollectionResource.logger.debug("Could not instantiate class", e);
			throw new IllegalStateException(e);
		}
		return newInstance;
	}
	
}