Skip to main content
summaryrefslogtreecommitdiffstats
blob: 14200d4c35827ca69a8d39e13a9598da6dffd0a2 (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.ejb.internal.impl;

import java.util.List;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jem.java.JavaHelpers;
import org.eclipse.jst.j2ee.ejb.CMPAttribute;
import org.eclipse.jst.j2ee.ejb.CommonRelationshipRole;
import org.eclipse.jst.j2ee.ejb.ContainerManagedEntity;
import org.eclipse.jst.j2ee.ejb.EjbPackage;
import org.eclipse.jst.j2ee.ejb.IRoleShapeStrategy;

/**
 * Insert the type's description here.
 * Creation date: (11/6/2000 11:41:33 AM)
 * @author: Administrator
 */
public class EJB20FlattenedRoleShapeStrategy extends RoleShapeStrategy {
	/**
	 * FlattenedRoleShapeStrategy constructor comment.
	 */
	public EJB20FlattenedRoleShapeStrategy(CommonRelationshipRole aRole) {
		super(aRole);
	}
	@Override
	protected boolean canContinue() {
		return !busy && role.getName() != null && (role.isForward() ||
			(role.isMany() && (role.getOppositeAsCommonRole() == null ||
			role.getOppositeAsCommonRole().isMany())));
	}
	
	public boolean usesAttributeNamed(String attributeName) {
		if (attributeName != null) {
			int index = attributeName.indexOf(IRoleShapeStrategy.ATTRIBUTE_NAME_JOINER);
			String name = attributeName.substring(0, index);
			return getRole().getName().equals(name);
		}
		return false;
	}

	/**
	 * @see RoleShapeStrategy#reconcileAttributes(CommonRelationshipRole, String, List, List)
	 */
	@Override
	protected void reconcileAttributes(CommonRelationshipRole aRole, String attributeName, List aList, List computedNames) {
		
		Resource res = aRole.eResource();
		boolean dirtyFlag = res != null ? res.isModified() : false;
		try {		
			if (aRole.getOppositeAsCommonRole() != null) {
				ContainerManagedEntity entity = aRole.getOppositeAsCommonRole().getSourceEntity();
		//	ContainerManagedEntityExtension roleType = (ContainerManagedEntityExtension)getTypeExtension(aRole);
			if (entity != null)
				collectAttributes(entity, attributeName, aList, computedNames);
		}
		} finally {
			if (res != null)
				res.setModified(dirtyFlag);
		}
	}

	/**
	 * @see com.ibm.ejs.models.base.extensions.ejbext.impl.FlattenedRoleShapeStrategy#setDerivedAttributeType(CMPAttribute, JavaHelpers)
	 */
	protected void setDerivedAttributeType(CMPAttribute attribute, CMPAttribute targetAttribute, boolean isNew) {
		if (targetAttribute == null) return;
		JavaHelpers fieldType = targetAttribute.getType();
		if (fieldType != null) {
			int singleRoleNameSize = getRole().getName().length() + targetAttribute.getName().length() + 1; //one for the underscore
			if (!(attribute.getName().length() > singleRoleNameSize)) //don't set the originating type since we are derived from another derived attribute
				attribute.setOriginatingType(fieldType);
			attribute.setEType(fieldType.getWrapper());
		}
		if (isNew && !getRole().isKey())
			attribute.setDerived(true);
	}
	/**
	 * getFields method comment.
	 */
	protected void collectAttributes(ContainerManagedEntity type, String attributeName, List aList, List computedNames) {
		collectKeyModelledAttributes(type, attributeName, aList, computedNames);
		collectKeyRoleAttributes(type, attributeName, aList, computedNames);
	}
	
	/**
	 * @deprecated to be safe, really should use collectAttributes(ContainerManagedEntity entity, CMPAttribute type, String attributeName, List aList, List computedNames)
	 * @param type
	 * @param attributeName
	 * @param aList
	 * @param computedNames
	 */
	protected void collectAttributes(CMPAttribute type, String attributeName, List aList, List computedNames) {
		// This is necessary for code generation
		ContainerManagedEntity entity = (ContainerManagedEntity) type.eContainer();
		collectAttributes(entity, type, attributeName, aList, computedNames);
	}
	
	/**
	 * 
	 */
	protected void collectAttributes(ContainerManagedEntity entity, CMPAttribute type, String attributeName, List aList, List computedNames) {
		boolean isNewAttribute = false;
		attributeName = appendName(attributeName, type.getName());
		computedNames.add(attributeName);
		CMPAttribute attribute = getCMPEntity().getPersistentAttribute(attributeName);
		if (attribute == null) {
			attribute = createPersistentAttribute(attributeName);
			isNewAttribute = true;
		}
		if (entity != null)
			setDerivedAttributeType(attribute, type, isNewAttribute);
		if (isNewAttribute) {
			if (getRole().isKey())
				getCMPEntity().getKeyAttributes().add(attribute);
			aList.add(attribute);
			getCMPEntity().getPersistentAttributes().add(attribute);
		} else {
			if (!aList.contains(attribute))
				aList.add(attribute);
		}
	}
	
	protected CMPAttribute createPersistentAttribute(String aName) {
		CMPAttribute attribute = ((EjbPackage)EPackage.Registry.INSTANCE.getEPackage(EjbPackage.eNS_URI)).getEjbFactory().createCMPAttribute();
		attribute.setName(aName);
		attribute.setDescription("Generated to support relationships.  Do NOT delete.");
		return attribute;
	}
	/**
	 * getFields method comment.
	 */
	protected void collectKeyModelledAttributes(ContainerManagedEntity entity, String attributeName, List aList, List computedNames) {
		java.util.Iterator it = entity.getFilteredFeatures(ModelledKeyAttributeFilter.singleton()).iterator();
		CMPAttribute attribute;
		while (it.hasNext()) {
			attribute = (CMPAttribute) it.next();
			collectAttributes(entity, attribute, attributeName, aList, computedNames);
		}
	}
	/**
	 * getFields method comment.
	 */
	protected void collectKeyRoleAttributes(ContainerManagedEntity entity, String attributeName, List aList, List computedNames) {
		if(visitedKeyTypes.contains(entity)){
			throw new RuntimeException("Key role cycle detected");
		}
		visitedKeyTypes.add(entity);
		java.util.Iterator it = entity.getFilteredFeatures(KeyRelationshipRoleFilter.singleton()).iterator();
		CommonRelationshipRole aRole;
		while (it.hasNext()) {
			aRole = (CommonRelationshipRole) it.next();
			String attName = appendName(attributeName, aRole.getName());
			reconcileAttributes(aRole, attName, aList, computedNames);
		}
	}
	protected ContainerManagedEntity getCMPEntity() {
		return getRole().getSourceEntity();
	}


}

Back to the top