I'm sorry I couldn't think of a more precise title, but hopefully I'll manage to explain the problem here. I have an Object (DataType) which acts something like the HTML SELECT element, it's a container for predefined values (DataTypeValues) . The values are set in a specific order and in the database, the primary key consists of the DataTypeValues Value and the Position. When displayed on the JSP, it looks something like this
1 First value
2 Second value
3 Third value
and so on. Now, the user has the option to rearrange the sort order by choosing any element to become the top or the bottom element (also to switch places but I got that one). Now, since the numbers are parts of the primary key, I can either add a new column DisplayOrder (which would be a pain in the ass because there's stored procedures and other modifications which I'd be happy to avoid), or do what I did for switching, and that is switching the data, eg. something like this
String temporarayValueContainer = firstDataTypeValue.getValue();
firstDataTypeValue.setValue(secondDataTypeValue.getValue());
secondDataTypeValue.setValue(temporarayValueContainer);
Now, I would like to do something similar, but I can't seem to nail the algorithm. Any ideas?
EDIT: So, here's something I tried:
public void setDomainPosition(SpisVrijDomene selected, String position, List vrijDomeneList) throws SpisException {
List sadrzajList = new ArrayList();
if (position.equals("TOP")) {
//I save the selected items as the first item in the list
sadrzajList.add(selected.getSadrzaj());
// I save all the other values, up to the selected value.
for (int i=1;i<vrijDomeneList.size();i++) {
SpisVrijDomene currentVrijDomene = (SpisVrijDomene)vrijDomeneList.get(i);
SpisVrijDomene previousVrijDomene = (SpisVrijDomene)vrijDomeneList.get(i-1);
sadrzajList.add(previousVrijDomene.getSadrzaj());
if (currentVrijDomene.equals(selected))
break;
}
}
else if (position.equals("BOTTOM")) {
sadrzajList.add(selected.getSadrzaj());
for (int i=vrijDomeneList.size()-1;i>0;i--) {
SpisVrijDomene currentVrijDomene = (SpisVrijDomene)vrijDomeneList.get(i);
SpisVrijDomene nextVrijDomene = (SpisVrijDomene)vrijDomeneList.get(i-1);
sadrzajList.add(nextVrijDomene.getSadrzaj());
if (currentVrijDomene.equals(selected))
break;
}
}
// Here I set the items new values function and return value
}
So this should work like this:
Start:
1 First value
2 Second value
3 Third value
4 Fourth value
5 Fifth value
Let's say the user selects Third Value to be Top. What the algorithm should do is:
sadrzajList.add("Third value");
sadrzajList.add("First value");
sadrzajList.add("Second value");
break;
for (int i=0; i<sadrzajList.size; i++)
vrijDomeneList.getItem(i).setValue(sadrzajList.getValue(i));