Merge "Inherit from oparent"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / _support / collections / withid / EntityWithIdCollectionResource.java
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.resources._support.collections.withid;
13
14 import java.lang.reflect.Constructor;
15 import java.util.List;
16
17 import org.eclipse.winery.repository.resources._support.IPersistable;
18 import org.eclipse.winery.repository.resources._support.collections.EntityCollectionResource;
19 import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public abstract class EntityWithIdCollectionResource<EntityResourceT extends EntityWithIdResource<EntityT>, EntityT> extends EntityCollectionResource<EntityResourceT, EntityT> {
24         
25         private static final Logger logger = LoggerFactory.getLogger(EntityWithIdCollectionResource.class);
26         
27         
28         /**
29          * {@inheritDoc}
30          */
31         public EntityWithIdCollectionResource(Class<EntityResourceT> entityResourceTClazz, Class<EntityT> entityTClazz, List<EntityT> list, IPersistable res) {
32                 super(entityResourceTClazz, entityTClazz, list, res);
33         }
34         
35         /**
36          * Each CollectionResource has to implement the id getting by itself as
37          * TOSCA XSD does not provide a general purpose id fetching mechanism
38          */
39         @Override
40         public abstract String getId(EntityT entity);
41         
42         @Override
43         protected EntityResourceT getEntityResourceInstance(EntityT entity, int idx) {
44                 Constructor<EntityResourceT> constructor;
45                 try {
46                         constructor = this.entityResourceTClazz.getConstructor(IIdDetermination.class, this.entityTClazz, int.class, List.class, this.res.getClass());
47                 } catch (Exception e) {
48                         try {
49                                 constructor = this.entityResourceTClazz.getConstructor(IIdDetermination.class, this.entityTClazz, int.class, List.class, IPersistable.class);
50                         } catch (Exception e2) {
51                                 EntityWithIdCollectionResource.logger.debug("Could not get constructor", e);
52                                 EntityWithIdCollectionResource.logger.debug("res.getClass() was {}", this.res.getClass());
53                                 throw new IllegalStateException(e2);
54                         }
55                 }
56                 EntityResourceT newInstance;
57                 try {
58                         newInstance = constructor.newInstance(this, entity, idx, this.list, this.res);
59                 } catch (Exception e) {
60                         EntityWithIdCollectionResource.logger.debug("Could not instantiate class", e);
61                         throw new IllegalStateException(e);
62                 }
63                 return newInstance;
64         }
65         
66 }