[Building Sakai] [Using Sakai] Dropdown list choice is not recovered by UsersAction.readUserForm

Rafael Morales Gamboa rmorales at suv.udg.mx
Thu Mar 14 17:40:30 PDT 2013


The code is in UsersAction.java, and I have not changed it from my first 
version, as I do not quite understand how information is actually put in 
the right place, only that it worked for INPUT but does not for SELECT.

*** UsersAction.java    2012-03-06 12:50:46.000000000 -0600
--- myUsersAction.java    2013-03-14 00:45:59.000000000 -0500
***************
*** 21,28 ****
--- 21,36 ----

   package org.sakaiproject.user.tool;

+ import java.io.BufferedReader;
+ import java.io.File;
+ import java.io.FileInputStream;
+ import java.io.IOException;
+ import java.io.InputStreamReader;
+ import java.io.UnsupportedEncodingException;
+
   import java.util.List;
   import java.util.ArrayList;
+ import java.util.HashMap;

   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
***************
*** 45,58 ****
--- 53,73 ----
   import org.sakaiproject.cheftool.menu.MenuEntry;
   import org.sakaiproject.cheftool.menu.MenuImpl;
   import org.sakaiproject.component.cover.ServerConfigurationService;
+ import org.sakaiproject.entity.api.ResourceProperties;
+ import org.sakaiproject.entity.api.ResourcePropertiesEdit;
   import org.sakaiproject.event.api.SessionState;
   import org.sakaiproject.event.cover.UsageSessionService;
+ import org.sakaiproject.exception.IdUnusedException;
+ import org.sakaiproject.exception.InUseException;
+ import org.sakaiproject.exception.PermissionException;
   import org.sakaiproject.thread_local.cover.ThreadLocalManager;
   import org.sakaiproject.tool.api.Session;
   import org.sakaiproject.tool.cover.SessionManager;
   import org.sakaiproject.user.api.Authentication;
   import org.sakaiproject.user.api.AuthenticationException;
   import org.sakaiproject.user.api.Evidence;
+ import org.sakaiproject.user.api.Preferences;
+ import org.sakaiproject.user.api.PreferencesEdit;
   import org.sakaiproject.user.api.User;
   import org.sakaiproject.user.api.UserAlreadyDefinedException;
   import org.sakaiproject.user.api.UserEdit;
***************
*** 61,66 ****
--- 76,82 ----
   import org.sakaiproject.user.api.UserNotDefinedException;
   import org.sakaiproject.user.api.UserPermissionException;
   import org.sakaiproject.user.cover.AuthenticationManager;
+ import org.sakaiproject.user.cover.PreferencesService;
   import org.sakaiproject.user.cover.UserDirectoryService;
   import org.sakaiproject.util.ExternalTrustedEvidence;
   import org.sakaiproject.util.RequestFilter;
***************
*** 75,80 ****
--- 91,105 ----
   public class UsersAction extends PagedResourceActionII
   {
       private static ResourceLoader rb = new ResourceLoader("admin");
+     private String organizationPropertyName = "prefs_organization";
+     private String categoryPropertyName = "prefs_category";
+     private String configDirectory = 
System.getProperty("catalina.base") + "/sakai/";
+     private String orgFileName = "organizations.txt";
+     private String roleFileName = "roles.txt";
+
+     private ArrayList<String> orgList = new ArrayList<String>();
+     private ArrayList<String> roleList = new ArrayList<String>();
+

       /**
        * {@inheritDoc}
***************
*** 239,244 ****
--- 264,302 ----
               template = buildListContext(state, context);
           }

+         try {
+             File file = new File(configDirectory + orgFileName);
+              BufferedReader in = new BufferedReader(
+                 new InputStreamReader(new FileInputStream(file), "UTF8"));
+             String s;
+
+             while ((s = in.readLine()) != null) {
+                 orgList.add(s);
+             }
+             in.close();
+
+             file = new File(configDirectory + roleFileName);
+              in = new BufferedReader(new InputStreamReader(new 
FileInputStream(file), "UTF8"));
+
+             while ((s = in.readLine()) != null) {
+                 roleList.add(s);
+             }
+             in.close();
+
+         }
+         catch (UnsupportedEncodingException e)
+         {
+             Log.warn("chef", e.getMessage());
+         }
+         catch (IOException e)
+         {
+             Log.warn("chef", e.getMessage());
+         }
+         catch (Exception e)
+         {
+             Log.warn("chef", e.getMessage());
+         }
+
           String prefix = (String) getContext(rundata).get("template");
           return prefix + template;

***************
*** 251,259 ****
       {
           // put the service in the context
           context.put("service", UserDirectoryService.getInstance());
!
           // put all (internal) users into the context
!         context.put("users", prepPage(state));

           // build the menu
           Menu bar = new MenuImpl();
--- 309,333 ----
       {
           // put the service in the context
           context.put("service", UserDirectoryService.getInstance());
!
!         List<User> users = (List<User>) prepPage(state);
!         HashMap<String,String> orgs = new HashMap();
!         HashMap<String,String> cats = new HashMap();
!
!         for (User u : users) {
!             try {
!                 String id = UserDirectoryService.getUserId(u.getEid());
! orgs.put(id,getUserProperty(state,id,organizationPropertyName));
! cats.put(id,getUserProperty(state,id,categoryPropertyName));
!             } catch (UserNotDefinedException e)
!             {
!             }
!         }
!
           // put all (internal) users into the context
!         context.put("users", users);
!         context.put("organizations", orgs);
!         context.put("categories", cats);

           // build the menu
           Menu bar = new MenuImpl();
***************
*** 328,334 ****

           context.put("incType", Boolean.valueOf(true));

!     context.put("superUser", 
Boolean.valueOf(SecurityService.isSuperUser()));

           String value = (String) state.getAttribute("valueEid");
           if (value != null) context.put("valueEid", value);
--- 402,408 ----

           context.put("incType", Boolean.valueOf(true));

!         context.put("superUser", 
Boolean.valueOf(SecurityService.isSuperUser()));

           String value = (String) state.getAttribute("valueEid");
           if (value != null) context.put("valueEid", value);
***************
*** 345,352 ****
           value = (String) state.getAttribute("valueType");
           if (value != null) context.put("valueType", value);

!         return "_edit";

       } // buildNewContext

       /**
--- 419,434 ----
           value = (String) state.getAttribute("valueType");
           if (value != null) context.put("valueType", value);

!         value = (String) state.getAttribute("valueOrganization");
!         if (value != null) context.put("valueOrganization", value);
!
!         value = (String) state.getAttribute("valueCategory");
!         if (value != null) context.put("valueCategory", value);

+         context.put("orgList", orgList);
+         context.put("roleList", roleList);
+
+         return "_edit";
       } // buildNewContext

       /**
***************
*** 375,381 ****

           value = (String) state.getAttribute("valueEmail");
           if (value != null) context.put("valueEmail", value);
!
           if ((Boolean)state.getAttribute("user.recaptcha-enabled"))
           {
               ReCaptcha captcha = 
ReCaptchaFactory.newReCaptcha((String)state.getAttribute("user.recaptcha-public-key"), 
(String)state.getAttribute("user.recaptcha-private-key"), false);
--- 457,469 ----

           value = (String) state.getAttribute("valueEmail");
           if (value != null) context.put("valueEmail", value);
!
!         value = (String) state.getAttribute("valueOrganization");
!         if (value != null) context.put("valueOrganization", value);
!
!         value = (String) state.getAttribute("valueCategory");
!         if (value != null) context.put("valueCategory", value);
!
           if ((Boolean)state.getAttribute("user.recaptcha-enabled"))
           {
               ReCaptcha captcha = 
ReCaptchaFactory.newReCaptcha((String)state.getAttribute("user.recaptcha-public-key"), 
(String)state.getAttribute("user.recaptcha-private-key"), false);
***************
*** 384,395 ****
               context.put("recaptchaScript", captchaScript);
           }

           return "_create";

       } // buildCreateContext

       /**
!      * Build the context for the new user mode.
        */
       private String buildEditContext(SessionState state, Context context)
       {
--- 472,486 ----
               context.put("recaptchaScript", captchaScript);
           }

+         context.put("orgList", orgList);
+         context.put("roleList", roleList);
+
           return "_create";

       } // buildCreateContext

       /**
!      * Build the context for the edit user mode.
        */
       private String buildEditContext(SessionState state, Context context)
       {
***************
*** 444,449 ****
--- 535,549 ----
           value = (String) state.getAttribute("valueType");
           if (value != null) context.put("valueType", value);

+         value = (String) state.getAttribute("valueOrganization");
+         if (value != null) context.put("valueOrganization", value);
+
+         value = (String) state.getAttribute("valueCategory");
+         if (value != null) context.put("valueCategory", value);
+
+         context.put("orgList", orgList);
+         context.put("roleList", roleList);
+
           return "_edit";

       } // buildEditContext
***************
*** 467,472 ****
--- 567,578 ----
               User user = UserDirectoryService.getUser(id);
               context.put("user", user);

+             String organization = getUserProperty(state, id, 
organizationPropertyName);
+             context.put("userOrganization", organization);
+
+             String category = getUserProperty(state, id, 
categoryPropertyName);
+             context.put("userCategory", category);
+
               // name the html form for user edit fields
               context.put("form-name", "user-form");

***************
*** 552,557 ****
--- 658,673 ----
           {
               UserEdit user = UserDirectoryService.editUser(id);
               state.setAttribute("user", user);
+             String organization = getUserProperty(state, id, 
organizationPropertyName);
+             if (organization != null) {
+                 state.setAttribute("userOrganization", organization);
+             }
+
+             String category = getUserProperty(state, id, 
categoryPropertyName);
+             if (category != null) {
+                 state.setAttribute("userCategory", category);
+             }
+
               state.setAttribute("mode", "edit");

               // disable auto-updates while not in list mode
***************
*** 607,612 ****
--- 723,738 ----
           {
               UserEdit user = UserDirectoryService.editUser(id);
               state.setAttribute("user", user);
+             String organization = getUserProperty(state, id, 
organizationPropertyName);
+             if (organization != null) {
+                 state.setAttribute("userOrganization", organization);
+             }
+
+             String category = getUserProperty(state, id, 
categoryPropertyName);
+             if (category != null) {
+                 state.setAttribute("userCategory", category);
+             }
+
               state.setAttribute("mode", "edit");

               // disable auto-updates while not in list mode
***************
*** 663,668 ****
--- 789,800 ----
               try
               {
                   UserDirectoryService.commitEdit(edit);
+                 String eid = (String) state.getAttribute("valueEid");
+                 String id = UserDirectoryService.getUserId(eid);
+                 String organization = (String) 
state.getAttribute("valueOrganization");
+                 setUserProperty(state, id, organizationPropertyName, 
organization);
+                 String category = (String) 
state.getAttribute("valueCategory");
+                 setUserProperty(state, id, categoryPropertyName, 
category);
               }
               catch (UserAlreadyDefinedException e)
               {
***************
*** 670,675 ****
--- 802,810 ----
                   Log.warn("chef", "UsersAction.doSave()" + e);
                   addAlert(state, rb.getString("useact.theuseid1"));
                   return;
+             } catch (UserNotDefinedException e) {
+                 addAlert(state, rb.getString("useprop.notset"));
+                 return;
               }
           }

***************
*** 677,682 ****
--- 812,823 ----
           if (user == null)
           {
               user = (User) state.getAttribute("newuser");
+
+             String id = user.getId();
+             String organization = (String) 
state.getAttribute("valueOrganization");
+             setUserProperty(state, id, organizationPropertyName, 
organization);
+             String category = (String) 
state.getAttribute("valueCategory");
+             setUserProperty(state, id, categoryPropertyName, category);
           }

           // cleanup
***************
*** 688,693 ****
--- 829,838 ----
           state.removeAttribute("valueLastName");
           state.removeAttribute("valueEmail");
           state.removeAttribute("valueType");
+         state.removeAttribute("valueOrganization");
+         state.removeAttribute("userOrganization");
+         state.removeAttribute("valueCategory");
+         state.removeAttribute("userCategory");

           // return to main mode
           state.removeAttribute("mode");
***************
*** 761,766 ****
--- 906,915 ----
           state.removeAttribute("valueLastName");
           state.removeAttribute("valueEmail");
           state.removeAttribute("valueType");
+         state.removeAttribute("valueOrganization");
+         state.removeAttribute("userOrganization");
+         state.removeAttribute("valueCategory");
+         state.removeAttribute("userCategory");

           // return to main mode
           state.removeAttribute("mode");
***************
*** 816,826 ****
           state.removeAttribute("user");
           state.removeAttribute("newuser");
           state.removeAttribute("new");
!                 state.removeAttribute("valueEid");
!                 state.removeAttribute("valueFirstName");
!                 state.removeAttribute("valueLastName");
!                 state.removeAttribute("valueEmail");
!                 state.removeAttribute("valueType");

           // go to main mode
           state.removeAttribute("mode");
--- 965,979 ----
           state.removeAttribute("user");
           state.removeAttribute("newuser");
           state.removeAttribute("new");
!         state.removeAttribute("valueEid");
!         state.removeAttribute("valueFirstName");
!         state.removeAttribute("valueLastName");
!         state.removeAttribute("valueEmail");
!         state.removeAttribute("valueType");
!         state.removeAttribute("valueOrganization");
!         state.removeAttribute("userOrganization");
!         state.removeAttribute("valueCategory");
!         state.removeAttribute("userCategory");

           // go to main mode
           state.removeAttribute("mode");
***************
*** 860,865 ****
--- 1013,1020 ----
           // Gateway New---null---false------true-------false
           // Account Edit--edit--true-------false------false

+         Log.info("chef", "UsersAction.readUserForm: " + data);
+
           // read the form
           String id = 
StringUtils.trimToNull(data.getParameters().getString("id"));
           String eid = 
StringUtils.trimToNull(data.getParameters().getString("eid"));
***************
*** 870,886 ****
           state.setAttribute("valueLastName", lastName);
           String email = 
StringUtils.trimToNull(data.getParameters().getString("email"));
           state.setAttribute("valueEmail", email);
           String pw = 
StringUtils.trimToNull(data.getParameters().getString("pw"));
           String pwConfirm = 
StringUtils.trimToNull(data.getParameters().getString("pw0"));
-
           String pwcur = 
StringUtils.trimToNull(data.getParameters().getString("pwcur"));

-         String mode = (String) state.getAttribute("mode");
           boolean singleUser = ((Boolean) 
state.getAttribute("single-user")).booleanValue();
           boolean createUser = ((Boolean) 
state.getAttribute("create-user")).booleanValue();

           boolean typeEnable = false;
           String type = null;
           if ((mode != null) && (mode.equalsIgnoreCase("new")))
           {
               typeEnable = true;
--- 1025,1045 ----
           state.setAttribute("valueLastName", lastName);
           String email = 
StringUtils.trimToNull(data.getParameters().getString("email"));
           state.setAttribute("valueEmail", email);
+         String organization = 
StringUtils.trimToNull(data.getParameters().getString("organization"));
+         state.setAttribute("valueOrganization", organization);
+         String category = 
StringUtils.trimToNull(data.getParameters().getString("category"));
+         state.setAttribute("valueCategory", category);
           String pw = 
StringUtils.trimToNull(data.getParameters().getString("pw"));
         String pwConfirm = 
StringUtils.trimToNull(data.getParameters().getString("pw0"));
         String pwcur = 
StringUtils.trimToNull(data.getParameters().getString("pwcur"));
+       String mode = (String) state.getAttribute("mode");

           boolean singleUser = ((Boolean) 
state.getAttribute("single-user")).booleanValue();
           boolean createUser = ((Boolean) 
state.getAttribute("create-user")).booleanValue();

           boolean typeEnable = false;
           String type = null;
+
           if ((mode != null) && (mode.equalsIgnoreCase("new")))
           {
               typeEnable = true;
***************
*** 959,964 ****
--- 1118,1154 ----
                   return false;
               }

+             //At CUDI we want all fields filled.
+             if (email == null)
+             {
+                 addAlert(state, rb.getString("usecre.emailmis"));
+                 return false;
+             }
+
+             if (firstName == null)
+             {
+                 addAlert(state, rb.getString("usecre.fnamemis"));
+                 return false;
+             }
+
+             if (lastName == null)
+             {
+                 addAlert(state, rb.getString("usecre.lnamemis"));
+                 return false;
+             }
+
+             if (organization == null)
+             {
+                 addAlert(state, rb.getString("usecre.orgmis"));
+                 return false;
+             }
+
+             if (category == null)
+             {
+                 addAlert(state, rb.getString("usecre.catmis"));
+                 return false;
+             }
+
               try
               {
                   // add the user in one step so that all you need is 
add not update permission
***************
*** 1003,1008 ****
--- 1193,1207 ----

                       // put the user in the state
                       state.setAttribute("user", user);
+
+                     String uorganization = getUserProperty(state, 
user.getId(), organizationPropertyName);
+                     if (uorganization != null) {
+ state.setAttribute("userOrganization", uorganization);
+                     }
+                     String ucategory = getUserProperty(state, 
user.getId(), categoryPropertyName);
+                     if (ucategory != null) {
+ state.setAttribute("userCategory", ucategory);
+                     }
                   }
                   catch (UserLockedException e)
                   {
***************
*** 1030,1042 ****
               user.setEmail(email);
               if (type != null) user.setType(type);

-       // make sure the old password matches, but don't check for super 
users
-       if (!SecurityService.isSuperUser()) {
-             if (!user.checkPassword(pwcur)) {
-               addAlert(state, rb.getString("usecre.curpass"));
-                       return false;
-             }
-       }

               if (mode == null || !mode.equalsIgnoreCase("remove")) {
                   // make sure we have matching password fields
--- 1229,1234 ----
***************
*** 1048,1055 ****

                   if (pw != null) user.setPassword(pw);
               }
-         }

           return true;
       }
   }
--- 1240,1312 ----

                   if (pw != null) user.setPassword(pw);
               }

+             //At CUDI we want all fields filled.
+             if (email == null)
+             {
+                 addAlert(state, rb.getString("usecre.emailmis"));
+                 return false;
+             }
+
+             if (firstName == null)
+             {
+                 addAlert(state, rb.getString("usecre.fnamemis"));
+                 return false;
+             }
+
+             if (lastName == null)
+             {
+                 addAlert(state, rb.getString("usecre.lnamemis"));
+                 return false;
+             }
+
+             if (organization == null)
+             {
+                 addAlert(state, rb.getString("usecre.orgmis"));
+                 return false;
+             }
+
+             if (category == null)
+             {
+                 addAlert(state, rb.getString("usecre.catmis"));
+                 return false;
+             }
+         }
           return true;
       }
+
+     private String getUserProperty (SessionState state, String userId, 
String pName) {
+
+         Preferences prefs;
+
+         Log.info("chef", "UsersAction.getUserProperty: " + userId);
+
+         try {
+             prefs = PreferencesService.getPreferences(userId);
+         } catch (Exception e) {
+             return null;
+         }
+
+         ResourceProperties props = prefs.getProperties();
+         return props.getProperty(pName);
+     }
+
+     private void setUserProperty (SessionState state, String userId, 
String pName, String pValue) {
+
+         PreferencesEdit prefs;
+
+         try {
+             try {
+                 prefs = PreferencesService.edit(userId);
+             } catch (Exception e) {
+                 // We assume any exception here means the preferences 
has not been created.
+                 prefs = PreferencesService.add(userId);
+             }
+             ResourcePropertiesEdit props = prefs.getPropertiesEdit();
+             props.addProperty(pName, pValue);
+             PreferencesService.commit(prefs);
+         } catch (Exception e) {
+             addAlert(state, rb.getString("useprop.notset") + " userId: 
" + userId + "Msg: " + e.getMessage());
+         }
+     }
   }


El 14/03/2013 12:16 a.m., Steve Swinsburg escribió:
> What's your code to get the param after the form is submitted? And what was the code for when it was working with the text field?
>
> Cheers
> Steve
>
>
> On 14/03/2013, at 16:11, Rafael Morales Gamboa <rmorales at suv.udg.mx> wrote:
>
>> Hello,
>>
>> I have added a new entry (organization) in the user registration form.
>> First it was an INPUT field, just as all others, and it did work fine.
>>
>>          <p class="shorttext required">
>>              <label for="organization">
>>                  #if(!$user)<span class="reqStar">*</span>#end
>> $tlang.getString("usecre.organization")
>>              </label>
>>              <input type="text" name="organization" id="organization"
>> #if($userOrganization)value="$validator.escapeHtml($userOrganization)"
>> #elseif($valueOrganization)value="$validator.escapeHtml($valueOrganization)"#end
>> />
>>          </p>
>>
>> Then I replaced it by a SELECT field
>>
>>          <p class="shorttext required">
>>              <label for="organization">
>>                  #if(!$user)<span class="reqStar">*</span>#end
>> $tlang.getString("usecre.organization")
>>              </label>
>>              <select name="organization" id="organization"/>
>>                  #foreach($item in $orgList)
>>                      <option value="$item"
>>                      #if($userOrganization &&
>> $item.equals($validator.escapeHtml($userOrganization)))selected="selected"
>>                      #elseif($valueOrganization &&
>> $item.equals($validator.escapeHtml($valueOrganization)))selected="selected"
>>                      #end/>$item</option>
>>                  #end
>>              </select>
>>          </p>
>>
>> and now it displays pretty but does not record the user choice.
>>
>> Any ideas? Some explanation of (or references to) how form data is
>> recovered?
>>
>> Regards,
>> Rafael
>>
>> _______________________________________________
>> sakai-user mailing list
>> sakai-user at collab.sakaiproject.org
>> http://collab.sakaiproject.org/mailman/listinfo/sakai-user
>>
>> TO UNSUBSCRIBE: send email to sakai-user-unsubscribe at collab.sakaiproject.org with a subject of "unsubscribe"



More information about the sakai-dev mailing list