1   package com.trendmicro.grid.acl.ds.jpa;
2   
3   import com.trendmicro.grid.acl.ds.PackageFamilyRepository;
4   import com.trendmicro.grid.acl.ds.jpa.entities.JpaPackageFamily;
5   import com.trendmicro.grid.acl.ds.jpa.entities.JpaVendor;
6   import com.trendmicro.grid.acl.l0.datatypes.NameListPage;
7   import com.trendmicro.grid.acl.l0.datatypes.PackageFamily;
8   import com.trendmicro.grid.acl.metadata.Metadata;
9   import org.springframework.stereotype.Repository;
10  import org.springframework.transaction.annotation.Transactional;
11  
12  import javax.annotation.Resource;
13  import javax.persistence.EntityManager;
14  import javax.persistence.PersistenceContext;
15  import java.util.List;
16  
17  import static com.trendmicro.grid.acl.ds.jpa.util.JpaUtils.toNameListPage;
18  import static net.sf.tinyjee.util.Assert.assertNotNull;
19  
20  /**
21   * Implements VendorRepository using JPA.
22   *
23   * @author Juergen_Kellerer, 2010-06-11
24   * @version 1.0
25   */
26  @Repository
27  @Transactional(readOnly = true)
28  public class JpaPackageFamilyRepository implements PackageFamilyRepository {
29  
30  	private static int pageSize = 1000;
31  
32  	public static int getPageSize() {
33  		return pageSize;
34  	}
35  
36  	public static void setPageSize(int pageSize) {
37  		JpaPackageFamilyRepository.pageSize = pageSize;
38  	}
39  
40  	@PersistenceContext(unitName = "CoreDB")
41  	EntityManager em;
42  
43  	@Resource
44  	JpaVendorRepository vendorRepository;
45  
46  	/**
47  	 * {@inheritDoc}
48  	 */
49  	@Transactional(readOnly = false)
50  	public JpaPackageFamily create(String vendorName, String basename, String displayName, Metadata metadata) {
51  		JpaPackageFamily family = getByName(basename);
52  		if (family == null)
53  			family = create(vendorRepository.create(vendorName, vendorName, null), basename, displayName, metadata);
54  		return family;
55  	}
56  
57  	@Transactional(readOnly = false)
58  	public JpaPackageFamily create(JpaVendor vendor, String basename, String displayName, Metadata metadata) {
59  		JpaPackageFamily family = new JpaPackageFamily(vendor, basename, displayName, metadata);
60  		em.persist(family);
61  		return family;
62  	}
63  
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	@Transactional(readOnly = false)
68  	public void update(PackageFamily packageFamily) {
69  		assertNotNull("packageFamily", packageFamily);
70  
71  		JpaPackageFamily f = getByName(packageFamily.getBasename());
72  
73  		if (f == null) {
74  			throw new IllegalArgumentException("Failed updating package family. " +
75  					"No entry exists with name '" + packageFamily.getBasename() + "'");
76  		}
77  
78  		f.setDisplayName(packageFamily.getDisplayName());
79  		f.setMetadata(packageFamily.getMetadata());
80  	}
81  
82  	/**
83  	 * {@inheritDoc}
84  	 */
85  	public JpaPackageFamily getByName(String basename) {
86  		List<JpaPackageFamily> result = em.createNamedQuery("PackageFamily.SelectByName", JpaPackageFamily.class).
87  				setParameter("basename", basename).getResultList();
88  		return result.isEmpty() ? null : result.get(0);
89  	}
90  
91  	/**
92  	 * Returns a lightweight reference used to satisfy foreign key constraints.
93  	 *
94  	 * @param basename the name of the package family.
95  	 * @return a lightweight reference used to satisfy foreign key constraints.
96  	 */
97  	public JpaPackageFamily getReferenceByName(String basename) {
98  		List<Integer> result = em.createNamedQuery("PackageFamily.SelectPrimaryKeyByName", Integer.class).
99  				setParameter("basename", basename).getResultList();
100 		return result.isEmpty() ? null : em.getReference(JpaPackageFamily.class, result.get(0));
101 	}
102 
103 	/**
104 	 * {@inheritDoc}
105 	 */
106 	public NameListPage listNames(int pageNumber) {
107 		return toNameListPage(em.createNamedQuery("PackageFamily.SelectNames", String.class), pageNumber, pageSize);
108 	}
109 
110 	/**
111 	 * {@inheritDoc}
112 	 */
113 	public NameListPage listNamesByVendor(String vendorName, int pageNumber) {
114 		return toNameListPage(em.createNamedQuery("PackageFamily.SelectNamesByVendor", String.class).
115 				setParameter("vendorName", vendorName), pageNumber, pageSize);
116 	}
117 }