samedi 31 mars 2007

Creation dynamic attributes

I solved a recurrent trouble when an entity need dynamics fields depending on it's own subcategory or class.

Here's my solution

db schema
DROP TABLE IF EXISTS attribute CASCADE;
CREATE TABLE attribute (
id_attribute SERIAL,
ref_subcategory integer REFERENCES subcategory(id_subcategory),
ref_message integer NOT NULL,
name varchar,
PRIMARY KEY (id_attribute)
);

DROP TABLE IF EXISTS value CASCADE;
CREATE TABLE value (
id_value SERIAL,
ref_attribute integer REFERENCES attribute(id_attribute),
ref_article integer,
value varchar NOT NULL,
PRIMARY KEY (id_value)
);

code from web tier :

Since my attributes depends on the subcategories I make a call to buildFields(1, 1) into it :

public void loadSubategories(int id) {
logger.info("loadSubategories Begin");
categoriesSelect.setContext(getContext());
categoriesSelect.bindRequestValue();
String categoryString = categoriesSelect.getValue();
if (categoryString != null && categoryString.length() > 0) {
int index = categoryString.indexOf("category=") + 9;
id = Integer.valueOf(categoryString.substring(index));
}
subcategoriesSelect.setContext(getContext());
subcategoriesSelect.bindRequestValue();
String subcategoryString = subcategoriesSelect.getValue();
Option selected = null;
if (subcategoryString != null && subcategoryString.length() > 0) {
int index = subcategoryString.indexOf("subcat=") + 7;
id = Integer.valueOf(subcategoryString.substring(index));
SubcategoryBU selectedCat = subcategoryService.findById(id);
selected = new Option("/auction/add_other.html?action=loadSubcat&category=" + id + "&subcat=",selectedCat.getText(lang));
selected.setSelected(true);
subcategoriesSelect.add(selected);
}
CategoryBU categoryBu = categoryService.findById(id, lang);
subcategories = subcategoryService.findAllSubcategory(categoryBu);
for (int i = 0; i < subcategory =" subcategories.get(i);" option =" new" action="loadSubcat&category="" subcat=" + subcategory.getSubcategoryId(),subcategory.getText(lang)); subcategoriesSelect.add(option); } subcategoriesSelect.setAttribute(" idcat = " + idCat + " idsubcat =" " categorystring =" categoriesSelect.getValue();"> 0) {
int index = categoryString.indexOf("category=") + 9;
idCat = Integer.valueOf(categoryString.substring(index));
}
subcategoriesSelect.setContext(getContext());
subcategoriesSelect.bindRequestValue();
String subcategoryString = subcategoriesSelect.getValue();
if (subcategoryString != null && subcategoryString.length() > 0) {
int index = subcategoryString.indexOf("subcat=") + 7;
idSubcat = Integer.valueOf(subcategoryString.substring(index));
}
if (idCat > 1 && idSubcat == 1)
idSubcat = categoryService.findFirstSubcategoryId(idCat);
AttributeBusinessServiceImpl attributeBusinessService = new AttributeBusinessServiceImpl();
List attributes = attributeBusinessService.findAllBySubcategory(idSubcat, lang);
for (int i = 0; i < attribute =" attributes.get(i);" name =" attribute.getText(lang).replace('" textfieldatribute =" new"> attributes = attributeBusinessService.findAllBySubcategory(idSubcat, lang)

Setting up a web project using eclipse

For my current project, I choose to use :

  • Click framework
  • PostgreSQL
  • MyEclipse
The first step is to create an empty project. From eclipse choose, create java project, then specify the src folder (choose to create a new one named src).

Then choose from the myeclipse menu the add web project functionality. This will add the required lib for a J2EE project.

Then add the click framework librairies, click.jar and click-extras. Add the postgresql jdbc driver by right clicking your base project, configure build path -> Add external Jar.

Then pick up the jar file from the postgresql jdbc folder.

Once you have done this, you could create your dao, db entity, business code and create the web layer.