views:

45

answers:

1

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));
A: 

Probably, you need sort array by DisplayOrder after any change. So, use some wrapper that will be detected all changes. Other variant: sort before access.

Stas
Hmm, not sure if i misunderstood you or you misunderstood me :) What I need is a concrete algorithm, eg, i'm in the method, I have DataTypeValues list, I know which item has been selected and I know if it needs to be at to top or bottom. The question is how? :)
Andrija
@Andrija : Hm.. Lets try again) You need swap to values, but you don't know how implement methods "set/getValue"?.
Stas
No, I need an algorithm. My example of indirect switching is an algorithm, now i need an algorithm for this; it should be language independent, i can translate pseudocode to java. Will post my attempt tommorow.
Andrija