[jbosstools-commits] JBoss Tools SVN: r43518 - workspace/akazakov/db/org.jboss.tools.cdi.db/META-INF and 2 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Sep 7 15:16:10 EDT 2012


Author: akazakov
Date: 2012-09-07 15:16:10 -0400 (Fri, 07 Sep 2012)
New Revision: 43518

Modified:
   trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/ICDICache.java
   workspace/akazakov/db/org.jboss.tools.cdi.db/META-INF/MANIFEST.MF
   workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/CDIDataBase.java
   workspace/akazakov/db/org.jboss.tools.common.db/src/org/jboss/tools/common/db/AbstractDBManager.java
Log:
https://issues.jboss.org/browse/JBIDE-12446

Modified: trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/ICDICache.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/ICDICache.java	2012-09-07 19:06:44 UTC (rev 43517)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.core/src/org/jboss/tools/cdi/core/ICDICache.java	2012-09-07 19:16:10 UTC (rev 43518)
@@ -21,7 +21,7 @@
 	 * @param project
 	 * @param bean
 	 */
-	public void rebuild(ICDIProject project, Collection<IBean> bean);
+	public void rebuild(ICDIProject project, Collection<IBean> beans);
 
 	/**
 	 * Returns all beans that has 'legalType' as one of legal types.
@@ -32,4 +32,4 @@
 	 */
 	public Collection<IBean> getBeansByLegalType(ICDIProject project, String legalType);
 
-}
+}
\ No newline at end of file

Modified: workspace/akazakov/db/org.jboss.tools.cdi.db/META-INF/MANIFEST.MF
===================================================================
--- workspace/akazakov/db/org.jboss.tools.cdi.db/META-INF/MANIFEST.MF	2012-09-07 19:06:44 UTC (rev 43517)
+++ workspace/akazakov/db/org.jboss.tools.cdi.db/META-INF/MANIFEST.MF	2012-09-07 19:16:10 UTC (rev 43518)
@@ -7,7 +7,8 @@
 Require-Bundle: org.eclipse.ui,
  org.eclipse.core.runtime,
  org.jboss.tools.common.db,
- org.jboss.tools.cdi.core
+ org.jboss.tools.cdi.core,
+ org.jboss.tools.common.el.core
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Bundle-Vendor: Red Hat, Inc.

Modified: workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/CDIDataBase.java
===================================================================
--- workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/CDIDataBase.java	2012-09-07 19:06:44 UTC (rev 43517)
+++ workspace/akazakov/db/org.jboss.tools.cdi.db/src/org/jboss/tools/cdi/db/CDIDataBase.java	2012-09-07 19:16:10 UTC (rev 43518)
@@ -10,25 +10,126 @@
  ******************************************************************************/
 package org.jboss.tools.cdi.db;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+
+import org.jboss.tools.cdi.core.IBean;
+import org.jboss.tools.cdi.core.ICDICache;
+import org.jboss.tools.cdi.core.ICDIProject;
+import org.jboss.tools.cdi.core.IQualifier;
 import org.jboss.tools.cdi.db.entity.BeanEntity;
+import org.jboss.tools.cdi.db.entity.QualifierEntity;
+import org.jboss.tools.cdi.db.entity.TypeEntity;
+import org.jboss.tools.common.java.IParametedType;
 
 /**
  * @author Alexey Kazakov
  */
-public class CDIDataBase {
+public class CDIDataBase implements ICDICache {
 
-	private static final CDIDataBase INSTANCE = new CDIDataBase();
+	private Map<String, Short> projects = new HashMap<String, Short>();
+	private short nextIndex = 0;
 
-	private CDIDataBase() {
-	}
+	/*
+	 * (non-Javadoc)
+	 * @see org.jboss.tools.cdi.core.ICDICache#rebuild(org.jboss.tools.cdi.core.ICDIProject, java.util.Collection)
+	 */
+	@Override
+	public void rebuild(ICDIProject project, Collection<IBean> beans) {
+		Short projectIndex = removeProjectIndex(project);
+		EntityManager em = CDIDBManager.getInstance().getEntityManager();
+		EntityTransaction transaction = em.getTransaction();
+		transaction.begin();
+		if(projectIndex!=null) {
+			em.createQuery("DELETE FROM BeanEntity b WHERE b.projectIndex = :p").setParameter("p", projectIndex).executeUpdate();
+			em.createQuery("DELETE FROM QualifierEntity b WHERE b.projectIndex = :p").setParameter("p", projectIndex).executeUpdate();
+			em.createQuery("DELETE FROM TypeEntity b WHERE b.projectIndex = :p").setParameter("p", projectIndex).executeUpdate();
+		}
+		projectIndex = generateProjectIndex(project);
 
-	public static CDIDataBase getInstance() {
-		return INSTANCE;
+		Map<Integer, QualifierEntity> qualifiers = new HashMap<Integer, QualifierEntity>();
+		Map<Integer, TypeEntity> types = new HashMap<Integer, TypeEntity>();
+		for (IBean bean : beans) {
+			BeanEntity beanEntity = new BeanEntity();
+			beanEntity.setIndex(bean.getId());
+			beanEntity.setProjectIndex(projectIndex);
+
+			Collection<IQualifier> beanQualifiers = bean.getQualifiers();
+			List<QualifierEntity> qualifierEntities = new ArrayList<QualifierEntity>();
+			for (IQualifier beanQualifier : beanQualifiers) {
+				int id = beanQualifier.getId();
+				QualifierEntity qualifierEntity = qualifiers.get(id);
+				if(qualifierEntity==null) {
+					qualifierEntity = new QualifierEntity();
+					qualifierEntity.setIndex(id);
+					qualifierEntity.setProjectIndex(projectIndex);
+					em.persist(qualifierEntity);
+				}
+				qualifierEntities.add(qualifierEntity);
+			}
+
+			Collection<IParametedType> beanTypes = bean.getLegalTypes();
+			List<TypeEntity> typeEntities = new ArrayList<TypeEntity>();
+			for (IParametedType beanType : beanTypes) {
+				int id = 0;
+				TypeEntity typeEntity = types.get(id);
+				if(typeEntity==null) {
+					typeEntity = new TypeEntity();
+					typeEntity.setIndex(id);
+					typeEntity.setProjectIndex(projectIndex);
+					em.persist(typeEntity);
+				}
+				typeEntities.add(typeEntity);
+			}
+
+			beanEntity.setQualifiers(qualifierEntities);
+			beanEntity.setTypes(typeEntities);
+			em.persist(beanEntity);
+		}
+		transaction.commit();
 	}
 
-	public Collection<BeanEntity> getBeans() {
+	/*
+	 * (non-Javadoc)
+	 * @see org.jboss.tools.cdi.core.ICDICache#getBeansByLegalType(org.jboss.tools.cdi.core.ICDIProject, java.lang.String)
+	 */
+	@Override
+	public Collection<IBean> getBeansByLegalType(ICDIProject project, String legalType) {
+		Short projectIndex = getProjectIndex(project);
+		EntityManager em = CDIDBManager.getInstance().getEntityManager();
+		EntityTransaction transaction = em.getTransaction();
+		transaction.begin();
+
+		em.createQuery("DELETE FROM BeanEntity b WHERE b.projectIndex = :p").setParameter("p", projectIndex).executeUpdate();
+
+		transaction.commit();
 		return null;
 	}
+
+	private Short getProjectIndex(ICDIProject project) {
+		String projectName = project.getNature().getProject().getName();
+		return projects.get(projectName);
+	}
+
+	private Short removeProjectIndex(ICDIProject project) {
+		String projectName = project.getNature().getProject().getName();
+		Short index = projects.get(projectName);
+		if(index!=null) {
+			projects.remove(projectName);
+		}
+		return index;
+	}
+
+	private short generateProjectIndex(ICDIProject project) {
+		String projectName = project.getNature().getProject().getName();
+		short index = nextIndex;
+		projects.put(projectName, nextIndex++);
+		return index;
+	}
 }
\ No newline at end of file

Modified: workspace/akazakov/db/org.jboss.tools.common.db/src/org/jboss/tools/common/db/AbstractDBManager.java
===================================================================
--- workspace/akazakov/db/org.jboss.tools.common.db/src/org/jboss/tools/common/db/AbstractDBManager.java	2012-09-07 19:06:44 UTC (rev 43517)
+++ workspace/akazakov/db/org.jboss.tools.common.db/src/org/jboss/tools/common/db/AbstractDBManager.java	2012-09-07 19:16:10 UTC (rev 43518)
@@ -98,6 +98,9 @@
 			return;
 		}
         try {
+        	if(em!=null && !em.isOpen()) {
+        		em.close();
+        	}
         	emf.close();
             DriverManager.getConnection(url);
         } catch (SQLException e) {



More information about the jbosstools-commits mailing list