source: cats/ISOcat/trunk/mod-ISOcat-interface-gi/interface/JSXAPPS/ISOcat/js/org/isocat/gui/Register.js @ 3447

Last change on this file since 3447 was 3447, checked in by mwindhouwer, 11 years ago

Made the ISOcat API more RESTful:

  • create resources using POST
  • update resources using PUT

One outlier: we use POST for creating users eventhough the client in this case specifies the full resource URL
Other outliers: in many cases we allow a dummy 0 for a POST call, e.g., POST /dcs/0 to create a new DCS

File size: 5.9 KB
Line 
1jsx3.util.Logger.getLogger("org.isocat").info("Loading class[org.isocat.gui.Register]");
2
3/**
4 * The registration controller
5 * @author Menzo.Windhouwer@mpi.nl
6 */
7jsx3.lang.Class.defineClass("org.isocat.gui.Register", null, [], function(Register, Register_prototype) {
8       
9        /**
10         * The class logger
11         */
12        Register.log = jsx3.util.Logger.getLogger("org.isocat.gui.Register");
13       
14        /**
15         * The root block of the component
16         */
17        Register_prototype.view;
18       
19        /**
20         * The CDF mapper
21         */
22        Register_prototype.cdf;
23       
24        /**
25         * The user name field
26         */
27        Register_prototype.login;
28       
29        /**
30         * The email field
31         */
32        Register_prototype.email;
33       
34        /**
35         * The first name field
36         */
37        Register_prototype.firstName;
38       
39        /**
40         * The last name field
41         */
42        Register_prototype.lastName;
43       
44        /**
45         * The organization field
46         */
47        Register_prototype.organization;
48       
49        /**
50         * The international phone number field
51         */
52        Register_prototype.phone;
53       
54        /**
55         * The postal address field
56         */
57        Register_prototype.post;
58       
59        /**
60         * The help tabs
61         */
62        Register_prototype.help;
63       
64        /**
65         * The register button
66         */
67        Register_prototype.registerButton;
68       
69        /**
70         * Constructor
71         */
72        Register_prototype.init = function(view) {
73                org.isocat.gui.Register.log.info("initialize Register["+this+"]");
74
75                this.view = view;
76                org.isocat.gui.Register.log.info("- register view["+this.view+"]");
77                this.cdf = this.view.getDescendantOfName("cdf");
78                org.isocat.gui.Register.log.info("- cdf mapper["+this.cdf+"]");
79                this.login = this.view.getDescendantOfName("loginField")
80                org.isocat.gui.Register.log.info("- login["+this.login+"]");
81                this.email = this.view.getDescendantOfName("emailField")
82                org.isocat.gui.Register.log.info("- email["+this.email+"]");
83                this.firstName = this.view.getDescendantOfName("firstNameField")
84                org.isocat.gui.Register.log.info("- first name["+this.firstName+"]");
85                this.lastName = this.view.getDescendantOfName("lastNameField")
86                org.isocat.gui.Register.log.info("- first name["+this.firstName+"]");
87                this.organization = this.view.getDescendantOfName("organizationField")
88                org.isocat.gui.Register.log.info("- organization["+this.organization+"]");
89                this.phone = this.view.getDescendantOfName("phoneField")
90                org.isocat.gui.Register.log.info("- international phone number["+this.phone+"]");
91                this.post = this.view.getDescendantOfName("postField")
92                org.isocat.gui.Register.log.info("- postal address["+this.post+"]");
93                this.help = this.view.getDescendantOfName("help")
94                org.isocat.gui.Register.log.info("- help["+this.help+"]");
95                this.registerButton = this.view.getDescendantOfName("register");
96                org.isocat.gui.Register.log.info("- register["+this.registerButton+"]");
97               
98                var cacheid = this.view.getId()+"-cdf";
99                var doc = jsx3.xml.CDF.Document.newDocument();
100                doc.insertRecord({jsxid:"new"});
101                org.isocat.APP.getCache().setDocument(cacheid,doc);
102                this.cdf.setXMLId(cacheid);
103                this.cdf.setCDFId("new");
104
105                org.isocat.gui.Register.log.info("initialized Register["+this+"]");
106               
107                this.view.controller = this;
108               
109                // publish the message using PageBus
110                org.isocat.publish("org.isocat.gui.Register.init", this);
111        };
112       
113        /**
114         * Set focus to the first field
115         */
116        Register_prototype.focus = function() {
117                this.view.focus();
118                // TIBCO hack: first the dialog needs to claim focus, then we can move the focus to the user name field, the sleep takes care of the delay
119                jsx3.sleep(function() { this.login.focus();},"focusRegister",this,true);
120        };
121       
122        /**
123         * Trigger the register action
124         */
125        Register_prototype.triggerRegister = function() {
126                this.registerButton.doExecute();
127        };
128       
129        /**
130         * Complete the registration
131         */
132        Register_prototype.complete = function() {
133                if (!this.login.doValidate()) {
134                        this.help.setSelectedIndex(1,true);
135                        this.login.focus();
136                        org.isocat.gui.Register.log.info("invalid login");
137                } else if (!this.email.doValidate()) {
138                        this.help.setSelectedIndex(1,true);
139                        this.email.focus();
140                        org.isocat.gui.Register.log.info("invalid email");
141                } else {
142                        org.isocat.gui.Loading.load(this.view,"Registering ...").show();
143                       
144                        this.disclaimerSubscription = window.PageBus.subscribe("org.isocat.gui.Disclaimer.accept",this,this.register,null);
145
146                        var disclaimer = org.isocat.APP.getRootBlock().loadAndCache("components/Disclaimer.xml",true);
147                        disclaimer.focus();
148                        //disclaimer.controller.focus();
149                }
150        };
151       
152        /**
153         * Try to register the new user
154         */
155        Register_prototype.register = function() {
156                window.PageBus.unsubscribe(this.disclaimerSubscription);
157                var user = this.login.getValue();
158               
159                this.cdf.write();
160                var doc = this.cdf.getXML();
161                var res = org.isocat.transformXML(doc,"CDF2User_xsl");
162                if (res == null) {
163                        org.isocat.gui.Register.log.info("failed to transform new user["+user+"] profile from CDF!");
164                        this.help.setSelectedIndex(3,true);
165                        org.isocat.gui.Loading.load(this.view).hide();
166                } else {
167                        var uri = "http://isocat:8080/isocat/rest/user/"+escape(user)+".xml";
168               
169                        //NOTE: as the URL of the new user is fully specified this should be a PUT instead of a POST, but this is an outlier to the rest of the API
170                        var req = new jsx3.net.Request().open("POST",uri,false);
171                        req.setRequestHeader("Content-Type","application/xml");// IE doesn't set this automagically (FF does), and otherwise NK won't pass it on to the ISOcat accessors
172                        req.send(res.toString());
173               
174                        if (req.getStatus()==201) {
175                                org.isocat.gui.Register.log.info("created user["+user+"] profile");
176                                this.view.doClose();
177                                org.isocat.APP.alert("Your registration was successful.","You will receive an email containing your password and your account details at your email address.",null,"OK")
178                        } else {
179                                org.isocat.gui.Register.log.info("failed to create user["+this._login+"] profile: "+req.getStatus()+"["+req.getStatusText()+"]");
180                                this.help.setSelectedIndex(3,true);
181                                org.isocat.gui.Loading.load(this.view).hide();
182                        }
183                }
184        };
185});
Note: See TracBrowser for help on using the repository browser.