Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I want to modify one list (actually info stored in a database). A person doesn't have access to update every part of the list, but can update and delete parts they do have access to and add whatever they want to the list.

Here's my pseudocode:

ModifiedList 
OriginalList
OriginalListWithUserRights

for each Thing in ModifiedList do
  if Thing in OriginalList then
    //Leave Along - nothing to change
  else
    OriginalList.add(Thing)

for each Thing in OriginalList do
  if (Thing in ModifiedList) then
    //LeaveAlone - nothing to change (same as above)
  else 
    for each RightsThing in OriginalListWithUserRights do
       if RightsThing not in OriginalList then
         //Leave Alone - don't remove things you don't have rights to
       else 
         OriginalList.Remove(RightsThing)

OriginalList.ReIndex() //because there will be some gaps

So, I guess this would work, but I think it's not right because it's just an obvious solution. I don't need to start with three lists as my inputs to this function. I could look up particular pieces of information while I'm iterating through my modified list. It just seems to structured and not object oriented enough for 2014.

One problem I have is that I'm going to be adding everything to the end of the list, seems like I should be adding things relative to where they were in the passed in list, but in a way that doesn't mess up the order of things that are not in my OriginalListWithUserRights.

share|improve this question
    
Looks fine to me. If you had an alternate way of doing it, what would you expect it to look like? –  Robert Harvey Feb 27 at 18:18
    
What does "clunky" mean? If we are to give you a "better" solution, how do we measure our relative degree of "clunkiness" against yours? –  Robert Harvey Feb 27 at 18:54
    
@RobertHarvey preserving the relative order of the merged lists is important. People are going to see things in a listbox in my program, save their screen and see all the things they changed at the bottom –  Peter Turner Feb 27 at 18:54

1 Answer 1

You could extend/wrap your Thing class in a ModifiedThing that includes a HowModified enum with values Create, Remove, Update, and None, and then you can do something like:

foreach ModifiedThing in ModifiedList
    thing = ModifiedThing.Thing
    switch ModifiedThing.HowModified
        case Create:
            if UserHasRightsToCreate
                OriginalList.InsertAt(ModifiedThing.index, thing)
        case Delete:
            if UserHasRightsToDelete(thing)
                OriginalList.Remove(thing)
        case Update:
            if UserHasRightsToUpdate(thing)
                OriginalList[thing.Key] = thing
        case None:
            //leave it alone
share|improve this answer
    
Thanks, that might be the best way of doing it - coworker suggested the same thing to me. I'll have to add HowModified to like 9000 objects, but oh well. –  Peter Turner Feb 27 at 19:06
    
I don't know what language you're working in, but you could try to create a generic ModifiedThing wrapper like ModifiedThing<T> that could accept any object and then provide the enum. –  Roger Feb 27 at 19:08
    
Another option would be to use the Decorator pattern to attach a Modifier object to the classes you need to add the functionality to; depending on how your object schema is set up, this might be a valid alternative. –  Roger Feb 27 at 19:18
    
This code will be in Java, the problem is the list is modified in Delphi and marshalled/bound into XML and unmarshalled in Java. I can load up the original list in java as well as the original rights list in java. Oh well, this is seems like a good solution to me. The description of the decorator pattern really seems like it fits the bill. –  Peter Turner Feb 27 at 19:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.