Commit 2fb8b76b authored by Kjetil Thuen's avatar Kjetil Thuen

Some extra comments and predifined code to make the tasks more simple.

As discussed on pre-Eddi meeting.
parent 36e65ab7
......@@ -2,3 +2,5 @@ target/
.classpath
.project
.settings
tags
*.swp
......@@ -21,7 +21,7 @@
<dependency>
<groupId>com.nesstar</groupId>
<artifactId>nesstar-api</artifactId>
<version>0.5</version>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
......
......@@ -7,6 +7,8 @@ import org.junit.Test;
import com.nesstar.api.NesstarDB;
import com.nesstar.api.NesstarDBFactory;
import com.nesstar.api.Server;
import java.net.URI;
import java.util.Locale;
/**
* Nesstar API Tutorial Level 1
......@@ -26,20 +28,25 @@ import com.nesstar.api.Server;
public class Level01 {
@Test
public void connectToNesstarServer() {
Server server = null;
assertEquals("Tutorial", server.getId());
}
public void connectToNesstarServer() {
Server server = null;
try {
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Set the preferred language to English.
* You need to use the NesstarDB.
*/
@Test
public void setPreferredLanguage() {
NesstarDB db = null;
public void setPreferredLanguage() {
NesstarDB db = null;
assertEquals(db.getPreferredLanguages()[0].getLanguage(), "en");
}
assertEquals(db.getPreferredLanguages()[0].getLanguage(), "en");
}
}
......@@ -3,142 +3,175 @@ package com.nesstar.tutorial;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import com.nesstar.api.IllegalTreeOperationException;
import com.nesstar.api.NesstarDB;
import com.nesstar.api.NesstarDBFactory;
import com.nesstar.api.NesstarObject;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Server;
import com.nesstar.api.Study;
import com.nesstar.api.Variable;
import com.nesstar.api.cube.Cube;
import com.nesstar.api.search.SearchQuery;
import com.nesstar.api.search.SearchResult;
import com.nesstar.api.search.SearchResultItem;
/**
* Nesstar API Tutorial Level 2
* ============================
* Nesstar API Tutorial Level 2 ============================
*
* This tutorial will teach you about searching.
*
* You need to connect to the server as you learned
* on Level 1.
*
* Also, this tutorial is in English. If your environment uses
* another language than that you need to change the
* language to English. This can be done in two ways:
* - Use the technique you learned on Level 1 to set the preferred
* language on a server.
* - Call Locale.setDefault(Locale.ENGLISH)
* You need to connect to the server as you learned on Level 1.
*
* Also, this tutorial is in English. If your environment uses another language
* than that you need to change the language to English. This can be done in two
* ways: - Use the technique you learned on Level 1 to set the preferred
* language on a server. - Call Locale.setDefault(Locale.ENGLISH)
*/
public class Level02 {
private Server server;
/**
* Here you can connect to the server once and for all.
* Remember to set the language to English here as well if
* you need to.
* Here you can connect to the server once and for all. Remember to set the
* language to English here as well if you need to.
*/
@Before
public void connectToServer() {
server = null;
try {
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Do a simple search for the term "vegetables". Retrieve one
* of the results.
*
* Do a simple search for the term "vegetables". Retrieve one of the results.
*/
@Test
public void performSimpleSearch() {
SearchQuery query = new SearchQuery();
SearchResultItem<NesstarObject> object = null;
assertTrue(object.getLabel().indexOf("vegetables") > -1);
SearchResultItem<NesstarObject> firstSearchHit = null;
try {
SearchQuery query = new SearchQuery();
} catch (Exception e) {
e.printStackTrace();
}
assertTrue(firstSearchHit.getLabel().indexOf("vegetables") > -1);
}
/**
* Now search for the term "vegetable" again.
* This time, tell the search query to return Studies only.
*
* Now search for the term "vegetable" again. This time, tell the search
* query to return Studies only.
*/
@Test
public void searchForStudy() {
SearchQuery query = new SearchQuery();
SearchResult result = null;
try {
SearchQuery query = new SearchQuery();
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(1, result.getListOfHits().size());
NesstarObject study = result.getListOfHits().get(0).getObject();
assertTrue(study instanceof Study);
}
/**
* If your search returns many hits, you can tell the search query to
* limit the number of results. Try searching for the term "health" and
* limit the number of results to 5 hits.
* If your search returns many hits, you can tell the search query to limit
* the number of results. Try searching for the term "health" and limit the
* number of results to 5 hits.
*
* Please not that there is currently a bug in the API that will potentially
* give the wrong number of results. Try asking for 4 hits to make the test
* pass.
*/
@Test
public void limitNumberOfSearchResults() {
SearchQuery query = new SearchQuery();
SearchResult result = null;
try {
SearchQuery query = new SearchQuery();
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(5, result.getListOfHits().size());
}
/**
* This time you need to do a more complex search. The term to search for
* is "medicine" but you must use the exclude method to narrow your search
* down to 1 result.
* This time you need to do a more complex search. The term to search for is
* "medicine" but you must use the exclude method to narrow your search down
* to 1 result.
*
* Tip: You can call exclude more than one time.
*/
@Test
public void searchWithBooleanOperators() {
SearchQuery query = new SearchQuery();
SearchResult result = null;
try {
SearchQuery query = new SearchQuery();
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(1, result.getListOfHits().size());
}
/**
* Now you must find all studies that are about the topic "health".
*
* The search query can be limited to certain parts of the DDI. Limit
* this search to the element "topcClas" (2.2.1.2).
* The search query can be limited to certain parts of the DDI. Limit this
* search to the element "topcClas" (2.2.1.2).
*
* Note: The XPath for the element in question is
* "//stdyDscr/stdyInfo/subject/topcClas"
*/
@Test
public void searchInSpecificDDIelements() {
SearchQuery query = new SearchQuery();
SearchResult result = null;
try {
SearchQuery query = new SearchQuery();
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(1, result.getListOfHits().size());
// If you're up to it try to extract the value of
// the topic classification from the result element.
}
/**
* Finally, sing the knowledge from the previous lessons, you must find
* 4 variables that have something to do with "school".
* Finally, sing the knowledge from the previous lessons, you must find 4
* variables that have something to do with "school".
*
* Please not that there is currently a bug in the API that will potentially
* give the wrong number of results. Try asking for 5 hits to make the test
* pass.
*/
@Test
public void findOnlyVariables() {
SearchQuery query = new SearchQuery();
SearchResult result = null;
List<SearchResultItem> listOfResults = result.getListOfHits();
assertEquals(4, listOfResults.size());
List<SearchResultItem> listOfResults = null;
try {
SearchQuery query = new SearchQuery();
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < 4; i++) {
NesstarObject item = listOfResults.get(i).getObject();
assertTrue(item instanceof Variable);
......
......@@ -6,7 +6,6 @@ import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Locale;
......@@ -14,6 +13,7 @@ import org.junit.Before;
import org.junit.Test;
import com.nesstar.api.Bank;
import com.nesstar.api.NesstarDB;
import com.nesstar.api.NesstarDBFactory;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Server;
......@@ -23,61 +23,93 @@ import com.nesstar.api.Variable;
import com.nesstar.api.cube.Cube;
/**
* Nesstar API Tutorial Level 3
* ============================
* Nesstar API Tutorial Level 3 ============================
*
* When you already know what to get you go to the Bank.
*
* In the Nesstar API, Banks are objects that you can use to get
* objects (studies, variables, cubes etc) you already know the ID
* of.
* In the Nesstar API, Banks are objects that you can use to get objects
* (studies, variables, cubes etc) you already know the ID of.
*
* Banks are retrieved from the server object. When fetching a Bank object you
* also state what type of objects you want the Bank to offer.
*
* Banks are retrieved from the server object. When fetching a Bank
* object you also state what type of objects you want the Bank to
* offer.
*
*/
public class Level03 {
private Server server;
/**
* Connect to the server once and for all. Remember the language.
* Connect to the server once and for all. Remember logging in and setting
* the language.
*/
@Before
public void connectToServer() {
server = null;
try {
server = null;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Take a Bank object that knows about studies and get the study
* with the ID "NSD1907".
* Take a Bank object that knows about studies and get the study with the ID
* "NSD1907".
*
* Note the exceptions thrown!
*/
@Test
public void getAStudyFromTheBank() throws NotAuthorizedException, IOException {
Study study = null;
try {
} catch (NotAuthorizedException nae) {
throw nae;
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
e.printStackTrace();
}
assertTrue(study.getLabel().indexOf("health and health care") > -1);
}
/**
* Now do as above and get the cube with the ID "nedpa2012_C1".
* Now do as above and get the cube with the ID "nedpa2012_C1".
*/
@Test
public void getACube() throws NotAuthorizedException, IOException {
Cube cube = null;
try {
} catch (NotAuthorizedException nae) {
throw nae;
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
e.printStackTrace();
}
assertEquals("Netherlands: Parliamentary Election 2012", cube.getLabel());
}
/**
* You can also get users from the bank. Get a list of all users on the server.
* You can also get users from the bank. Get a list of all users on the
* server.
*/
@Test
public void getUser() throws NotAuthorizedException, IOException {
List<User> users = null;
try {
} catch (NotAuthorizedException nae) {
throw nae;
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(5, users.size());
}
......@@ -87,8 +119,14 @@ public class Level03 {
*/
@Test
public void printAllVariables() {
List<Study> allVariables = null;
List<Variable> allVariables = null;
try {
} catch (Exception e) {
e.printStackTrace();
}
assertNotNull(allVariables);
}
}
......@@ -3,88 +3,91 @@ package com.nesstar.tutorial;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import javax.xml.xpath.XPathExpressionException;
import org.junit.Before;
import org.junit.Test;
import com.nesstar.api.Bank;
import com.nesstar.api.DdiElement;
import com.nesstar.api.DdiList;
import com.nesstar.api.NesstarDB;
import com.nesstar.api.NesstarDBFactory;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Server;
import com.nesstar.api.SimpleDdiElement;
import com.nesstar.api.Study;
/**
* Nesstar API Tutorial Level 4
* ============================
* Nesstar API Tutorial Level 4 ============================
*
* In this tutorial you will lean about retrieving DDI metadata
* from a Study.
* In this tutorial you will lean about retrieving DDI metadata from a Study.
*
* Working with metadata in the Nesstar API is mostly like working
* with XML and XPath. All references to DDI fields in this tutorial
* can be related to the XML document and thus translated into XPath.
* Working with metadata in the Nesstar API is mostly like working with XML and
* XPath. All references to DDI fields in this tutorial can be related to the
* XML document and thus translated into XPath.
*
* All references to DDI elements are also denoted by their index
* value (e.g. 4.0 for dataDscr).
* All references to DDI elements are also denoted by their index value (e.g.
* 4.0 for dataDscr).
*
* The method getElement takes an XPath expression (a String) as input
* and returns the element(s) that match it.
* The method getElement takes an XPath expression (a String) as input and
* returns the element(s) that match it.
*
* Remember that Nesstar currently uses DDI version 1.2.2.
*
*
*/
public class Level04 {
private Server server;
private Study study;
/**
* Connect to the server and get the study with
* ID "NSD1907".
* Connect to the server and get the study with ID "NSD1907".
*/
@Before
public void connectToServerAndGetStudy() {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Get the title element (2.1.1.1) and get the string value.
*
* Note: The xpath for the title element is
* "./stdyDscr/citation/titlStmt/titl"
*/
@Test
public void findTitle() {
String title = null;
assertTrue(title.indexOf("International survey on health and health care") > -1);
}
/**
* Get the ID of the data file (3.0).
*/
@Test
public void getTheFileID() {
String id = null;
assertEquals("NSD1907.NSDstat", id);
try {
} catch (Exception e) {
e.printStackTrace();
}
assertTrue(title.indexOf("International survey on health and health care") > -1);
}
/**
* Find the keywords (2.2.1.1) and make a list of them.
*
* Make sure to keep them in order.
*
* Note: The xpath for the keyword elements is "//stdyInfo/subject/keyword"
*/
@Test
public void getKeywords() {
List<String> keywords = null;
try {
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(9, keywords.size());
assertEquals("Economics", keywords.get(4));
}
......
......@@ -3,18 +3,15 @@ package com.nesstar.tutorial;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import com.nesstar.api.Bank;
import com.nesstar.api.Category;
import com.nesstar.api.NesstarDB;
import com.nesstar.api.NesstarDBFactory;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Server;
import com.nesstar.api.Study;
import com.nesstar.api.Variable;
......@@ -28,58 +25,74 @@ import com.nesstar.api.analysis.TableCoordinates;
import com.nesstar.api.analysis.TabulationBuilder;
/**
* Nesstar API Tutorial Level 5
* ============================
* Nesstar API Tutorial Level 5 ============================
*
* This tutorial is about analysis. You will learn about how to
* do tabulations, regression, and correlation using the API.
* This tutorial is about analysis. You will learn about how to do tabulations,
* regression, and correlation using the API.
*
* Analysis uses a builder to collect the input. Tabulation uses
* TabulationBuilder to which you can add break variables, measure
* subset, weights etc. Regression and correlation use
* LinearRegressionBuilder and CorrelationBuilder.
* TabulationBuilder to which you can add break variables, measure subset,
* weights etc. Regression and correlation use LinearRegressionBuilder and
* CorrelationBuilder.
*
* The result of a tabulation is returned as a Table. To get the values from it
* you create a TableCoordinate where you add the variables and categories you
* wish to get the value for.
*
* The result of a tabulation is returned as a Table. To get the
* values from it you create a TableCoordinate where you add the
* variables and categories you wish to get the value for.
*
*/
public class Level05 {
private Server server;
private Study study;
/**
* Fetch the Study with ID "NSD1907".
*/
@Before
public void connectToServerAndGetStudy() {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Do a tabulation with one break variable and one measure just
* to get the feel of it.
* Do a tabulation with one break variable and one measure just to get the
* feel of it.
*
* Pick any two variables. For example NSD1907_V1 and NSD1907_V2
*/
@Test
public void runSimpleTabulation() {
Table table = null;
try {
} catch (Exception e) {
e.printStackTrace();
}
assertNotNull(table);
}
/**
* Tabulate the variables gender (id NSD1907_V65) and Confidence
* in the health care system in Norway (id NSD1907_V3).
* Then get the number of cases for 'females' with 'no confidence
* at all.'
* Tabulate the variables gender (id NSD1907_V65) and Confidence in the
* health care system in Norway (id NSD1907_V3). Then get the number of cases
* for 'females' with 'no confidence at all.'
*/
@Test
public void tabulateTwoVariables() {
double cellValue = Double.NaN;
assertEquals(123d, cellValue, 0d);
try {
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(13d, cellValue, 0d);
}
/**
* Do a tabulation using the variable 'Last 12 months: visited or been
* visited by a doctor' (NSD1907_V38) as a break variable and 'Age groups'
......@@ -90,16 +103,23 @@ public class Level05 {
@Test
public void tabulateWithStatistics() {
double sum = Double.NaN;
assertEquals(123d, sum, 0d);
try {
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(264d, sum, 0d);
}
/**
* Run a regression analysis with Current marital status (NSD1907_V111)
* as the dependent variable and Highest completed education (NSD1907_V76)
* as independent variable.
* Run a regression analysis with Current marital status (NSD1907_V111) as
* the dependent variable and Highest completed education (NSD1907_V76) as
* independent variable.
*
* Then retrieve the values for case count, significance, R, and standard coefficient.
* Then retrieve the values for case count, significance, R, and standard
* coefficient.
*/
@Test
public void runRegressionanalysis() {
......@@ -107,16 +127,22 @@ public class Level05 {
double significance = Double.NaN;
double r = Double.NaN;
double standardCoefficient = Double.NaN;