aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Modifying DataInfo Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Modifying DataInfo" Watch "Modifying DataInfo" New topic
Author

Modifying DataInfo

Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Hi Ranchers
I have addeed a new method in DataInfo class to retrieve the value of a particular field in a record:
/**
* This method returns a String, which reflect the
* value of the field in this DataItem passed as parameter .
*
* @param fieldname The field name to match upon to find the field value.
* @return fieldname field value string
* of the fieldname passed as params.
* @throws DatabaseException Thrown if fieldname is not valid.
*/
public String getValue(String fieldname) throws DatabaseException
{
String strFieldValue = null;
for (int i = 0; i < fields.length; i++)
{
String strField = (String)fields[i].getName();

if(strField.equals(fieldname))
{
strFieldValue = values[i].trim();
break;
}
}

if(strFieldValue == null)
throw new DatabaseException("Invalid field name specified");
else
return strFieldValue;
}
i am using this method at various places. I have create a booking info panel which displays every detail of the selected flight. and has textfield for specifying the no of seats required. This panel has labels for each details e.g label for flight no selected, carrier of the flight etc
Is this correct ??
Amit
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Tough question. It comes down to being able to defend you decision to change that class. Still we don't know if that by itself will not have them reduce points for your score.
I personally decided that unless they said change the class that they provided, that I wouldn't. I believe that there are other ways to get that information with very little code, and without changing the DataInfo class.
But it is always down to your decision and remembering to defend it in your design.txt file.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Thanks Mark
But what are the other ways of doing it ??
Amit
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

I created a getFieldData method in my DataAccessFacade. In it I pass it a string of the fieldname, and a DataInfo object. I then loop through the fields array and field names array returned by the getFields and getValues methods and when the field name matches the field, then I get the corresponding field value.
This is probably exactly how you did it in the DataInfo class change, I just put the method outside it to the Facade.
Mark
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Mark
But in this case, if in future if we need to create another facade and want to get the value on the basis of field name, the same method needs to be replicated there which is not good (duplication of code basically).
Moreover this is a basic feature of getting a columnn values depending on column name like resultset have. So better to have it in DataInfo
I may be wrong but
What u say Mark ??
Amit
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Either way is fine. Yes you would do the old copy and paste with a new Facade, but if you are creating a new Facade, I see it as replacement instead of Inheritence, or Enhancement. So I wouldn't quite see there being a major problem with Cut and Paste here.
Just my 2 cents, and that is worth about 1.5 cents in Euros.
Mark
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
I approach the situation in neither way.
I created a new method in the Data class
/**
* Gets the index of the given field whose field name is specified
* by <code>fieldname</code>.
* @return the index of the field. If no such field, return -1.
* @param fieldname The name of the field to be search.
*/
protected int getIndexOfField(String fieldname) {
int Index = -1;
for (int i = 0; i < description.length; i++) {
if (description[i].getName().trim().equals(fieldname)) {
Index = i;
break;
}
}
return Index;
}
When it need to get the perticular field value, the index of the given field name is retrieved first then the value could be easily got by the straightforword array[index].
The method is frequently used in the criteriaFind method (reside in the Data class also)so it could not be put in the Facade class.
And i added another new method in Data class:
public Vector getFieldItems(String fieldName) throws DatabaseException
The method is used to populate the search combobox and invokes my getIndexOfField methed also.
And, in these methods, i used readRecord() method which return a string array rather than using the getRecord(int recNum) which return a DataInfo object.

Does my design make sense? Is the Data class the right place for these methods?

Regards
James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Modifying DataInfo