001    package org.hackystat.projectbrowser.authentication;
002    
003    import org.apache.wicket.markup.html.basic.Label;
004    import org.apache.wicket.markup.html.form.StatelessForm;
005    import org.apache.wicket.markup.html.form.TextField;
006    import org.apache.wicket.model.CompoundPropertyModel;
007    import org.apache.wicket.model.PropertyModel;
008    import org.hackystat.projectbrowser.ProjectBrowserApplication;
009    import org.hackystat.projectbrowser.ProjectBrowserSession;
010    import org.hackystat.sensorbase.client.SensorBaseClient;
011    import org.hackystat.sensorbase.client.SensorBaseClientException;
012    
013    /**
014     * The form for providing an email for registration purposes. 
015     * 
016     * @author Philip Johnson
017     */
018    class RegisterForm extends StatelessForm {
019    
020      /** Support serialization. */
021      private static final long serialVersionUID = 1L;
022      /** The email. */
023      private String email;
024    
025      /**
026       * Create this form, supplying the wicket:id.
027       * 
028       * @param id The wicket:id.
029       */
030      public RegisterForm(final String id) {
031        super(id);
032        setModel(new CompoundPropertyModel(this));
033        add(new TextField("email"));
034        add(new Label("registerFeedback", 
035            new PropertyModel(ProjectBrowserSession.get(), "registerFeedback")));
036    
037      }
038      
039      /**
040       * Performs the registration action upon submittal.
041       */
042      @Override
043      public void onSubmit() {
044        // Make sure there's an email address supplied.
045        if ((email == null) || "".equals(email)) {
046          ProjectBrowserSession.get().setRegisterFeedback("No email supplied");
047          return;
048        }
049        // Make sure the sensorbase can be contacted.
050        ProjectBrowserApplication app = (ProjectBrowserApplication)getApplication();
051        String sensorbase = app.getSensorBaseHost();
052        if (!SensorBaseClient.isHost(sensorbase)) {
053          ProjectBrowserSession.get().setRegisterFeedback(sensorbase + " not available.");
054          return;
055        }
056        // SensorBase is available, so register using it.
057        // Currently, registration takes longer than 10 seconds, so don't indicate the error. 
058        String msg;
059        try {
060          SensorBaseClient.registerUser(sensorbase, email);
061          msg = "Registration attempted. Please check your email for password information";
062          ProjectBrowserSession.get().setRegisterFeedback(msg);
063        }
064        catch (SensorBaseClientException e) {
065          msg = "Registration attempted. Please check your email for password information";
066          //msg = "Registration failed: " + e.getMessage() + " Contact your Hackystat admin.";
067          ProjectBrowserSession.get().setRegisterFeedback(msg);
068        }
069      }
070    
071      /**
072       * Returns the user.
073       * 
074       * @return The user.
075       */
076      public String getEmail() {
077        return email;
078      }
079    
080      /**
081       * Sets the user. 
082       * @param email The user email.
083       */
084      public void setEmail(String email) {
085        this.email = email;
086      }
087    }