1

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.

asked Feb 27, 2014 at 18:11
3
  • Looks fine to me. If you had an alternate way of doing it, what would you expect it to look like? Commented Feb 27, 2014 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? Commented Feb 27, 2014 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 Commented Feb 27, 2014 at 18:54

1 Answer 1

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
answered Feb 27, 2014 at 18:56
4
  • 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. Commented Feb 27, 2014 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. Commented Feb 27, 2014 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. Commented Feb 27, 2014 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. Commented Feb 27, 2014 at 19:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.