View Javadoc

1   /* ------------------------------------
2    * © BNP Paribas - SIG/CGI/SFRI - 2005
3    * ------------------------------------
4    * Projet  : projectName
5    * Fichier : Entity.java
6    * $Id$ 
7    * $Date$ 
8    * $Log$
9    * 
10   */
11  package com.atlassian.jira.util.entities;
12  
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  import org.ofbiz.core.entity.GenericEntityException;
17  import org.ofbiz.core.entity.GenericValue;
18  import org.ofbiz.core.util.UtilMisc;
19  
20  import com.atlassian.core.ofbiz.CoreFactory;
21  import com.atlassian.core.ofbiz.util.EntityUtils;
22  
23  /**
24   * @author BNP Paribas - SIG/CGI/SFRI - 2005
25   * <b>Description :</b>
26   * @version $Id$
27   * @history <ul>
28   * <li/>Date    		- UserId - Observations
29   * <li/>22 juin 2005 - 139611 - Initialisation de la classe.
30   * </ul>
31   * 
32   */
33  public abstract class Entity {
34  
35  
36  	private int id = 0;
37  	private String name;
38  	private String description;
39  	
40  	protected abstract String getType(); 
41  	
42  	protected void addFields(Map _fields) {
43  		_fields.put("id", "" + getId());
44  		_fields.put("name", getName());
45  		_fields.put("description", getDescription());
46  	}
47  	
48  	public boolean exist() throws GenericEntityException {
49  		return !(CoreFactory.getGenericDelegator().findByAnd(getType(), UtilMisc.toMap("id", String.valueOf(getId()))).size() == 0);
50  	}
51  
52  	public boolean doStorage() throws GenericEntityException {
53  		return true;
54  	}
55  
56  	protected abstract int getNewId() throws NumberFormatException, GenericEntityException; 
57  
58  	public GenericValue store() throws GenericEntityException{
59  		GenericValue gv = null;
60  		try {
61  			Map fields = new HashMap();
62  			if (getId()==0) {
63  				id = getNewId();
64  			}			
65  			addFields(fields);
66  			if (doStorage()) {
67  				if (exist()) {
68  					GenericValue v = CoreFactory.getGenericDelegator().makeValue(getType(), fields);
69  					v.store();
70  				} else {
71  					gv = EntityUtils.createValue(getType(), fields);
72  				}
73  			}
74  		} catch (Exception e) {
75  			throw new GenericEntityException("",e);
76  		}
77  		return gv;	
78  	}
79  	
80  	
81  	/**
82  	 * @return
83  	 */
84  	public int getId() {
85  		return id;
86  	}
87  
88  	/**
89  	 * @return
90  	 */
91  	public String getDescription() {
92  		return description;
93  	}
94  
95  	/**
96  	 * @return
97  	 */
98  	public String getName() {
99  		return name;
100 	}
101 
102 	/**
103 	 * @param i
104 	 */
105 	public void setId(int i) {
106 		id = i;
107 	}
108 
109 	/**
110 	 * @param string
111 	 */
112 	public void setDescription(String string) {
113 		description = string;
114 	}
115 
116 	/**
117 	 * @param string
118 	 */
119 	public void setName(String string) {
120 		name = string;
121 	}
122 
123 }