Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit to git repo, intended for public release.
- Loading branch information
0 parents
commit 4087b42
Showing
43 changed files
with
14,147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
## JavaScript Tutorial (for Java Developers) by [@johnwilander](https://twitter.com/johnwilander) | ||
|
||
A tutorial covering JavaScript design patterns, dependency handling and builds with [require.js](http://requirejs.org/), unit testing with [Jasmine](http://pivotal.github.com/jasmine/), simple DOM manipulation and Ajax calls with [jQuery](http://jquery.com/), and client-side encoding with [jQuery Encoder](https://github.com/chrisisbeef/jquery-encoder). | ||
|
||
## Brendan Eich's Kind Intro | ||
Brendan Eich, the creator of JavaScript and the CTO of Mozilla, was kind enough to record a short intro for this tutorial. [Check it out](http://videos-cdn.mozilla.net/serv/drafts/Brendan-60fps-720p-MPEG-4.webm). | ||
|
||
## The Slides | ||
The slides for this tutorial are [available on Slideshare](http://www.slideshare.net/johnwilander/javascript-fr-javautvecklare), currently only in Swedish. | ||
|
||
## The Backend | ||
The whole project is setup with Maven and is run on Jetty via the Jetty Maven plugin. The Java backend is built with Jersey JAX-RS and Spring DI. | ||
|
||
## Setup and Run | ||
- Make sure you have Maven installed. Try running 'mvn -version' in your shell to see if you've got it. | ||
- Download or fork the code, go to the source folder (where pom.xml can be found) and run 'mvn install'. Maven will now download the the interwebz for you :). | ||
- In the same folder, run 'mvn jetty:run'. | ||
- Surf to [http://localhost:8080/JavaScriptTutorial/](http://localhost:8080/JavaScriptTutorial/). | ||
- When you want to stop the server you run 'mvn jetty:stop'. | ||
|
||
# Try Out a JavaScript Build | ||
If you want to try running the require.js build including concatenation and minification of JavaScript, do this: | ||
- Make sure you have Rhino (js.jar) downloaded. In my example I have it in /Applications/Utilities/rhino/ on my Mac. | ||
- Make sure you have Google's Closure Compiler (compiler.jar) downloaded. In my example I have it in /Applications/Utilities/Closure_Compiler/ on my Mac. | ||
- In your shell, go to path_to_the_tutorial_code_on_your_machine/src/main/webapp/js/ | ||
- run java -classpath /Applications/Utilities/rhino/js.jar:/Applications/Utilities/Closure_Compiler/compiler.jar org.mozilla.javascript.tools.shell.Main ../../r.js -o name=base out=min.js baseUrl=. paths.jquery=empty: paths.jquery-encoder=empty: | ||
|
||
# Try Out JavaScript Unit Testing | ||
If you want to look into JavaScript unit testing with Jasmine: | ||
- surf to [http://localhost:8080/JavaScriptTutorial/test/JW/jasmineTestSpec.html](http://localhost:8080/JavaScriptTutorial/test/JW/jasmineTestSpec.html) (the live.js auto-refresh doesn't work in Chrome at them moment) | ||
- Check the corresponing source in path_to_the_tutorial_code_on_your_machine/src/main/webapp/test/JW/ | ||
|
||
## Contact | ||
Ping me on Twitter – [@johnwilander](https://twitter.com/johnwilander) – if you have questions or problems setting it up. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>JavaScriptTutorial</groupId> | ||
<artifactId>JavaScriptTutorial</artifactId> | ||
<packaging>war</packaging> | ||
<version>1.0</version> | ||
<name>JavaScriptTutorial Maven Webapp</name> | ||
<url>http://maven.apache.org</url> | ||
<properties> | ||
<jersey-version>1.11</jersey-version> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<dependencies> | ||
<!-- External dependencies --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring</artifactId> | ||
<version>2.5.6.SEC02</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
<version>1.1.1</version> | ||
</dependency> | ||
<!-- Jersey --> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-server</artifactId> | ||
<version>${jersey-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey</groupId> | ||
<artifactId>jersey-json</artifactId> | ||
<version>${jersey-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.jersey.contribs</groupId> | ||
<artifactId>jersey-spring</artifactId> | ||
<version>${jersey-version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.xml.bind</groupId> | ||
<artifactId>jaxb-impl</artifactId> | ||
<version>2.1.12</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.annotation</groupId> | ||
<artifactId>jsr250-api</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
<!-- Testing --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<finalName>JavaScriptTutorial</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.mortbay.jetty</groupId> | ||
<artifactId>jetty-maven-plugin</artifactId> | ||
<configuration> | ||
<stopKey>foo</stopKey> | ||
<stopPort>9999</stopPort> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.3.2</version> | ||
<configuration> | ||
<source>1.6</source> | ||
<target>1.6</target> | ||
</configuration> | ||
</plugin> </plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package se.johnwilander.medication; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
/** | ||
* @johnwilander | ||
*/ | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
@XmlRootElement | ||
public class Medication { | ||
private String name; | ||
private MedicationUnit unit; | ||
private float amount; | ||
|
||
Medication(String name, MedicationUnit unit, float amount) { | ||
this.name = name; | ||
this.unit = unit; | ||
this.amount = amount; | ||
} | ||
|
||
public Medication() { | ||
// Here for JAXB's sake | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Medication{" + | ||
"name='" + name + '\'' + | ||
", unit=" + unit + | ||
", amount=" + amount + | ||
'}'; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/se/johnwilander/medication/MedicationFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package se.johnwilander.medication; | ||
|
||
/** | ||
* @johnwilander | ||
*/ | ||
public class MedicationFactory { | ||
public Medication createMedication(String name, MedicationUnit unit, float amount) { | ||
return new Medication(name, unit, amount); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/se/johnwilander/medication/MedicationUnit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package se.johnwilander.medication; | ||
|
||
/** | ||
* @johnwilander | ||
*/ | ||
public enum MedicationUnit { | ||
GRAM("g"), MILLIGRAM("mg"), MILLILITER("ml"); | ||
|
||
private final String abbreviation; | ||
private MedicationUnit(String abbreviation) { | ||
this.abbreviation = abbreviation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package se.johnwilander.patient; | ||
|
||
import se.johnwilander.medication.Medication; | ||
import se.johnwilander.personnummer.Personnummer; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import java.util.List; | ||
|
||
/** | ||
* @johnwilander | ||
*/ | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
@XmlRootElement | ||
public class Patient { | ||
private Personnummer personnummer; | ||
private List<Medication> medications; | ||
|
||
// Don't use this constructor, use PatientFactory instead | ||
Patient(Personnummer personnummer, List<Medication> medications) { | ||
this.personnummer = personnummer; | ||
this.medications = medications; | ||
} | ||
|
||
public Personnummer getPersonnummer() { | ||
return personnummer; | ||
} | ||
|
||
public Patient() { | ||
// Here for JAXB's sake | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Patient{" + | ||
"personnummer=" + personnummer + | ||
", medications=" + medications + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package se.johnwilander.patient; | ||
|
||
import se.johnwilander.personnummer.Personnummer; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @johnwilander | ||
*/ | ||
public class PatientDb { | ||
private Map<Personnummer, Patient> patients = Collections.synchronizedMap(new HashMap<Personnummer, Patient>()); | ||
|
||
public Patient getPatient(Personnummer personnummer) { | ||
return patients.get(personnummer); | ||
} | ||
|
||
void putPatient(Patient patient) { | ||
patients.put(patient.getPersonnummer(), patient); | ||
} | ||
|
||
public PatientDb(List<Patient> initialPatients) { | ||
for(Patient patient : initialPatients) { | ||
putPatient(patient); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package se.johnwilander.patient; | ||
|
||
import se.johnwilander.medication.Medication; | ||
import se.johnwilander.personnummer.Personnummer; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @johnwilander | ||
*/ | ||
public class PatientFactory { | ||
public Patient createPatient(Personnummer personnummer, List<Medication> medications) { | ||
return new Patient(personnummer, medications); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/se/johnwilander/personnummer/BadPersonnummerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package se.johnwilander.personnummer; | ||
|
||
/** | ||
* @johnwilander | ||
*/ | ||
public class BadPersonnummerException extends Exception { | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/se/johnwilander/personnummer/Personnummer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package se.johnwilander.personnummer; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @johnwilander | ||
*/ | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
@XmlRootElement | ||
public class Personnummer { | ||
private String personnummerStr; | ||
|
||
private static final Pattern PERSONNUMMER_PATTERN = Pattern.compile("[0-9]*"); | ||
private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); | ||
|
||
private static Date OLDEST_ACCEPTABLE_DATE; | ||
static { | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.set(1880, 1, 1); | ||
OLDEST_ACCEPTABLE_DATE = calendar.getTime(); | ||
} | ||
|
||
public Personnummer(String personnummerStr) throws BadPersonnummerException { | ||
if (isValid(personnummerStr)) { | ||
this.personnummerStr = personnummerStr; | ||
} else { | ||
throw new BadPersonnummerException(); | ||
} | ||
} | ||
|
||
private boolean isValid(String personnummerStr) throws BadPersonnummerException { | ||
if(personnummerStr == null || personnummerStr.length() != 12 || !PERSONNUMMER_PATTERN.matcher(personnummerStr).matches()) { | ||
return false; | ||
} | ||
|
||
Date date = extractDate(personnummerStr); | ||
|
||
if(date.before(OLDEST_ACCEPTABLE_DATE) || date.after(new Date())) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private Date extractDate(String personnummerStr) throws BadPersonnummerException { | ||
try { | ||
synchronized (formatter) { // SimpleDateFormat not thread-safe | ||
return formatter.parse(personnummerStr.substring(0, 8)); | ||
} | ||
} catch (ParseException e) { | ||
System.err.println(personnummerStr + " produced a ParseException"); | ||
throw new BadPersonnummerException(); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return personnummerStr; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Personnummer that = (Personnummer) o; | ||
|
||
if (!personnummerStr.equals(that.personnummerStr)) return false; | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return personnummerStr.hashCode(); | ||
} | ||
|
||
public Personnummer() { | ||
// Here for JAXB's sake | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/se/johnwilander/personnummer/PersonnummerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package se.johnwilander.personnummer; | ||
|
||
/** | ||
* @johnwilander | ||
*/ | ||
public class PersonnummerFactory { | ||
|
||
public Personnummer createPersonnummer(String personnummerStr) { | ||
Personnummer personnummer; | ||
try { | ||
personnummer = new Personnummer(personnummerStr); | ||
} catch (BadPersonnummerException e) { | ||
System.err.println("Erroneous personnummer " + personnummerStr); | ||
personnummer = null; | ||
} | ||
return personnummer; | ||
} | ||
} |
Oops, something went wrong.