001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.rest.codegen.model;
043:
044: import org.netbeans.modules.websvc.rest.codegen.model.EntityClassInfo;
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.List;
050: import java.util.Map;
051: import java.util.Set;
052: import java.util.logging.Level;
053: import java.util.logging.Logger;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.modules.j2ee.persistence.api.metadata.orm.Entity;
056: import org.netbeans.modules.websvc.rest.codegen.model.EntityResourceBean.Type;
057: import org.netbeans.modules.websvc.rest.codegen.model.EntityClassInfo;
058: import org.netbeans.modules.websvc.rest.codegen.model.EntityClassInfo.FieldInfo;
059: import org.netbeans.modules.websvc.rest.wizard.Util;
060:
061: /**
062: *
063: * @author PeterLiu
064: */
065: public class EntityResourceModelBuilder {
066: private Map<String, EntityClassInfo> entityClassInfoMap;
067: EntityResourceBeanModel model;
068:
069: /** Creates a new instance of ModelBuilder */
070: public EntityResourceModelBuilder(Project project,
071: Collection<Entity> entities) {
072: entityClassInfoMap = new HashMap<String, EntityClassInfo>();
073: for (Entity entity : entities) {
074: EntityClassInfo info = new EntityClassInfo(entity, project,
075: this );
076: entityClassInfoMap.put(entity.getClass2(), info);
077: }
078: }
079:
080: public List<Entity> getEntities() {
081: List<Entity> ret = new ArrayList<Entity>();
082: for (EntityClassInfo info : entityClassInfoMap.values()) {
083: ret.add(info.getEntity());
084: }
085: return ret;
086: }
087:
088: public Set<EntityClassInfo> getEntityInfos() {
089: return new HashSet<EntityClassInfo>(entityClassInfoMap.values());
090: }
091:
092: public Set<String> getAllEntityNames() {
093: return entityClassInfoMap.keySet();
094: }
095:
096: public EntityClassInfo getEntityClassInfo(String type) {
097: return entityClassInfoMap.get(type);
098: }
099:
100: public EntityResourceBeanModel build(Collection<Entity> selected) {
101: model = new EntityResourceBeanModel(this );
102: try {
103: for (Entity entity : selected) {
104: EntityClassInfo info = entityClassInfoMap.get(entity
105: .getClass2());
106: getContainerResourceBean(info);
107: }
108:
109: model.setValid(true);
110:
111: } catch (Exception ex) {
112: Logger.getLogger(getClass().getName()).log(Level.ALL,
113: "build", ex);
114: model.setValid(false);
115: }
116:
117: return model;
118: }
119:
120: private EntityResourceBean getContainerResourceBean(
121: EntityClassInfo info) {
122: EntityResourceBean bean = model.getContainerResourceBean(info);
123:
124: if (bean == null) {
125: bean = createContainerResourceBean(info);
126: }
127:
128: return bean;
129: }
130:
131: private EntityResourceBean createContainerResourceBean(
132: EntityClassInfo info) {
133: EntityResourceBean containerBean = new EntityResourceBean(
134: Type.CONTAINER);
135:
136: String pluralName = Util.pluralize(info.getName());
137: containerBean.setName(pluralName);
138: containerBean.setEntityClassInfo(info);
139: containerBean.setUriTemplate("/"
140: + Util.lowerFirstChar(pluralName) + "/"); //NOI18N
141:
142: model.addContainerResourceBean(containerBean);
143:
144: EntityResourceBean itemBean = getItemResourceBean(info);
145:
146: containerBean.addSubResource(new RelatedEntityResource(
147: itemBean, info.getIdFieldInfo()));
148:
149: return containerBean;
150: }
151:
152: private EntityResourceBean getItemResourceBean(EntityClassInfo info) {
153: EntityResourceBean bean = model.getItemResourceBean(info);
154:
155: if (bean == null) {
156: bean = createItemResourceBean(info);
157: }
158:
159: return bean;
160: }
161:
162: private EntityResourceBean createItemResourceBean(
163: EntityClassInfo info) {
164: EntityResourceBean itemBean = new EntityResourceBean(Type.ITEM);
165:
166: itemBean.setName(Util.singularize(info.getName()));
167: itemBean.setEntityClassInfo(info);
168: FieldInfo idField = info.getIdFieldInfo();
169: String uriTemplate = "";
170:
171: if (!idField.isEmbeddedId()) {
172: uriTemplate = "{" + info.getIdFieldInfo().getName() + "}/"; //NOI18N
173: } else {
174: int count = 0;
175: for (FieldInfo field : idField.getFieldInfos()) {
176: if (count++ > 0) {
177: uriTemplate += ","; //NOI18N
178: }
179:
180: uriTemplate += "{" + field.getName() + "}"; //NOI18N
181: }
182:
183: uriTemplate += "/"; //NOI18N
184: }
185:
186: itemBean.setUriTemplate(uriTemplate);
187:
188: model.addItemResourceBean(itemBean);
189:
190: computeRelationship(itemBean, info);
191:
192: return itemBean;
193: }
194:
195: private void computeRelationship(EntityResourceBean bean,
196: EntityClassInfo info) {
197: for (FieldInfo fieldInfo : info.getFieldInfos()) {
198: if (fieldInfo.isRelationship()) {
199: EntityResourceBean foreignBean = null;
200: EntityResourceBean foreignItemBean = null;
201:
202: if (fieldInfo.isOneToMany() || fieldInfo.isManyToMany()) {
203: foreignBean = getContainerResourceBean(entityClassInfoMap
204: .get(fieldInfo.getTypeArg()));
205: foreignItemBean = getItemResourceBean(entityClassInfoMap
206: .get((fieldInfo.getTypeArg())));
207: } else {
208: foreignBean = getItemResourceBean(entityClassInfoMap
209: .get(fieldInfo.getType()));
210: }
211:
212: RelatedEntityResource subResource = new RelatedEntityResource(
213: foreignBean, fieldInfo);
214: bean.addSubResource(subResource);
215:
216: RelatedEntityResource super Resource = new RelatedEntityResource(
217: bean, fieldInfo);
218: foreignBean.addSuperResource(superResource);
219:
220: if (foreignItemBean != null) {
221: foreignItemBean.addSuperResource(superResource);
222: }
223: }
224: }
225: }
226: }
|