View Javadoc

1   package com.atlassian.jira.referentiel.entities;
2   
3   import java.util.List;
4   
5   import org.apache.log4j.Category;
6   import org.ofbiz.core.entity.EntityUtil;
7   import org.ofbiz.core.entity.GenericDelegator;
8   import org.ofbiz.core.entity.GenericEntityException;
9   import org.ofbiz.core.entity.GenericValue;
10  import org.ofbiz.core.util.UtilMisc;
11  
12  import com.atlassian.core.ofbiz.CoreFactory;
13  
14  public class DefaultExternalEntityManager implements IExternalEntityManager {
15  
16  	private final Category log = Category.getInstance(DefaultExternalEntityManager.class);
17  	
18  	/** 
19  	 * @return List of all entities (GenericValue)
20  	 * @throws GenericEntityException 
21  	 */
22  	public List getEntities(final String _entityName) throws GenericEntityException {
23  		GenericDelegator gd = CoreFactory.getGenericDelegator();
24  		List gvs = gd.findAll(_entityName); 
25     	return gvs;
26  	}
27  
28  	/** 
29  	 * @return a Entity identified by _id
30  	 * @throws GenericEntityException 
31  	 */
32  	public GenericValue getEntity(final String _entityName, final Object _id) throws GenericEntityException {
33  		GenericDelegator gd = CoreFactory.getGenericDelegator();
34  		GenericValue gv = EntityUtil.getOnly(gd.findByAnd(_entityName, UtilMisc.toMap("id",_id))); 
35     	return gv;
36  	}
37  
38  	/**
39  	 * @return an Entity's Field identified by _id. All Exception are caught
40  	 */
41  	public String getEntityFieldOrValue(String _entityName, Object _id, String _fieldName) {
42  		GenericValue entity;
43  		String fieldValue = _id.toString();
44  		try {
45  			entity = getEntity(_entityName,_id);
46  			fieldValue = entity.get(_fieldName).toString();
47  		} catch (Exception e) {
48  			log.error("Catching Exception in order to return Id." +  e.getMessage());
49  		}
50  		return fieldValue;
51  	}
52  
53  }