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