aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/java/org/eclipse/winery/repository/resources/_support/collections/withoutid/EntityWithoutIdCollectionResource.java
blob: 8255f5d8d3c9b4fc4f0a455e62e6f412d6f118e1 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*******************************************************************************
 * 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.withoutid;

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

import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.eclipse.winery.common.Util;
import org.eclipse.winery.repository.Utils;
import org.eclipse.winery.repository.resources.AbstractComponentInstanceResource;
import org.eclipse.winery.repository.resources._support.IPersistable;
import org.eclipse.winery.repository.resources._support.collections.EntityCollectionResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jersey.api.NotFoundException;

/**
 * Class managing a list of entities. It is intended to manage subresources,
 * where the TOSCA specification did not specify a unique key. Currently, the
 * hashCode of the XML String representation is used. If other representation
 * should be used, the method {@code getEntityResource} has to be overriden.
 * 
 * @param <EntityResourceT> the resource modeling the entity
 * @param <EntityT> the entity type of single items in the list
 */
public abstract class EntityWithoutIdCollectionResource<EntityResourceT extends EntityWithoutIdResource<EntityT>, EntityT> extends EntityCollectionResource<EntityResourceT, EntityT> {
	
	private static final Logger logger = LoggerFactory.getLogger(EntityWithoutIdCollectionResource.class);
	
	
	/**
	 * {@inheritDoc}
	 */
	public EntityWithoutIdCollectionResource(Class<EntityResourceT> entityResourceTClazz, Class<EntityT> entityTClazz, List<EntityT> list, IPersistable res) {
		super(entityResourceTClazz, entityTClazz, list, res);
	}
	
	/**
	 * Method searching the list for an id with the hashcode instead of
	 * getId(EntityT)
	 */
	@Override
	@Path("{id}/")
	public EntityResourceT getEntityResource(@PathParam("id") String id) {
		id = Util.URLdecode(id);
		int idInt;
		try {
			idInt = Integer.parseInt(id);
		} catch (java.lang.NumberFormatException e) {
			throw new NotFoundException(id + " is not a valid id");
		}
		EntityT entity = null;
		int idx = -1;
		for (EntityT c : this.list) {
			idx++;
			// speed optimization - instead of using getId() we directly use the hash code
			int hash = Utils.getXMLAsString(c).hashCode();
			if (hash == idInt) {
				entity = c;
				break;
			}
		}
		if (entity == null) {
			throw new NotFoundException();
		} else {
			return this.getEntityResourceInstance(entity, idx);
		}
	}
	
	@Override
	public String getId(EntityT entity) {
		return IdDeterminationWithHashCode.INSTANCE.getId(entity);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected EntityResourceT getEntityResourceInstance(EntityT entity, int idx) {
		Constructor<EntityResourceT> constructor;
		try {
			constructor = this.entityResourceTClazz.getConstructor(this.entityTClazz, int.class, List.class, AbstractComponentInstanceResource.class);
		} catch (Exception e) {
			try {
				constructor = this.entityResourceTClazz.getConstructor(this.entityTClazz, int.class, List.class, IPersistable.class);
			} catch (NoSuchMethodException | SecurityException e1) {
				EntityWithoutIdCollectionResource.logger.debug("Could not get constructor", e);
				throw new IllegalStateException(e);
			}
		}
		EntityResourceT newInstance;
		try {
			newInstance = constructor.newInstance(entity, idx, this.list, this.res);
		} catch (Exception e) {
			EntityWithoutIdCollectionResource.logger.debug("Could not instantiate class", e);
			throw new IllegalStateException(e);
		}
		return newInstance;
	}
	
}