Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Commit

Permalink
changes according to review, including issue #47, as well as a fix fo…
Browse files Browse the repository at this point in the history
…r the bug described in issue #53
  • Loading branch information
phil committed Dec 16, 2014
1 parent b3360be commit f28caaf
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 117 deletions.
39 changes: 20 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Compilation output
/target
/build

# Not sharing keys
config

# Vagrant files
/.vagrant

# IDE files
/.idea
/*.iml
/.metadata
/.project
/nb-configuration.xml
.classpath
.settings/
!/config/
# Compilation output
/target
/build

# Not sharing keys
config

# Vagrant files
/.vagrant

# IDE files
/.idea
/*.iml
/.metadata
/.project
/nb-configuration.xml
.classpath
.settings/
!/config/
/nbactions.xml
56 changes: 55 additions & 1 deletion src/main/java/de/ddb/pdc/core/PDCResult.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
package de.ddb.pdc.core;

import de.ddb.pdc.metadata.DDBItem;
import de.ddb.pdc.storage.StoredPDCResult;
import java.util.Date;
import java.util.List;

/**
* Represents the result of a public-domain calculation for a specific item.
*/
public class PDCResult {

private final String itemId;
private final Boolean publicDomain;
private final List<AnsweredQuestion> trace;
private final String itemCategory;
private final String institution;
private final Date createdDate;

/**
* Creates a PDCResult.
*
* @param publicDomain whether the item is considered public-domain
* @param trace trace of calculation questions and answers
* @param metadata properties of the item
*/
public PDCResult(Boolean publicDomain, List<AnsweredQuestion> trace) {
public PDCResult(Boolean publicDomain, List<AnsweredQuestion> trace,
DDBItem metadata) {

this.itemId = metadata.getId();
this.publicDomain = publicDomain;
this.trace = trace;
this.itemCategory = metadata.getCategory();
this.institution = metadata.getInstitution();
this.createdDate = new Date();
}

/**
* Alternative constructor used to create an instance of @{link PDCResult}
* from a @{link StoredPDCResult}.
*
* @param storedPDCResult record fetched from storage
*/
public PDCResult(StoredPDCResult storedPDCResult) {
this.itemId = storedPDCResult.getItemId();
this.publicDomain = storedPDCResult.isPublicDomain();
this.trace = storedPDCResult.getTrace();
this.itemCategory = storedPDCResult.getItemCategory();
this.institution = storedPDCResult.getInstitution();
this.createdDate = storedPDCResult.getCreatedDate();
}

/**
* @return the unique id of the cultural good.
*/
public String getItemId() {
return this.itemId;
}

/**
* Returns true if the item in question is considered public-domain by
* the calculator, or false if not. Returns null if the status could not be
Expand All @@ -43,5 +79,23 @@ public Boolean isPublicDomain() {
public List<AnsweredQuestion> getTrace() {
return trace;
}

/**
* @return the item category originating from @{link DDBItem}.
*/
public String getItemCategory() {
return this.itemCategory;
}

/**
* @return the institution's name originating from @{link DDBItem}.
*/
public String getInstitution() {
return this.institution;
}

public Date getCreatedDate() {
return createdDate;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public PublicDomainCalculatorImpl(
public PDCResult calculate(String country, DDBItem metadata) {
// FIXME: HardCoded
Category category = Category.LITERARY_OR_ARTISTIC_WORK;
Questionnaire questionnaire = questionnaireFactory.build(country, category);
Questionnaire questionnaire = questionnaireFactory.build(
country, category, metadata
);
answerQuestions(questionnaire, metadata);
return questionnaire.getResult();
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/de/ddb/pdc/core/Questionnaire.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.ddb.pdc.core;

import de.ddb.pdc.metadata.DDBItem;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
Expand All @@ -22,17 +23,20 @@ public class Questionnaire {
private FlowChartState state;
private final List<AnsweredQuestion> answeredQuestions;
private boolean isAborted;
private final DDBItem metadata;

/**
* Creates a new Questionnaire from the initial state of the flow chart given
* as parameter.
*
* @param initialState The initial state of the flow chart to use.
* @param metadata The DDBItem with the meta data about the cultural good
*/
public Questionnaire(FlowChartState initialState) {
public Questionnaire(FlowChartState initialState, DDBItem metadata) {
this.state = initialState;
this.answeredQuestions = new LinkedList<AnsweredQuestion>();
this.isAborted = false;
this.metadata = metadata;
}

/**
Expand Down Expand Up @@ -136,15 +140,17 @@ public List<AnsweredQuestion> getTrace() {

/**
* Get the result of this questionnaire. The result will contain the public
* domain status of the cultural good this questionnaire was answered for
* and the trace of all answered questions.
* domain status of the cultural good this questionnaire was answered for,
* the trace of all answered questions and the metadata of the cultural good.
*
* @return The public domain status and the answered question trace.
* @throws IllegalStateException You did not answer all the questions that are
* necessary. You can not get a result yet.
*/
public PDCResult getResult() throws IllegalStateException {
PDCResult result = new PDCResult(this.isPublicDomain(), this.getTrace());
PDCResult result = new PDCResult(
this.isPublicDomain(), this.getTrace(), this.metadata
);
return result;
}
}
8 changes: 6 additions & 2 deletions src/main/java/de/ddb/pdc/core/QuestionnaireFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.ddb.pdc.core;

import de.ddb.pdc.metadata.DDBItem;
import org.springframework.stereotype.Service;

/**
Expand All @@ -14,13 +15,16 @@ public class QuestionnaireFactory {
*
* @param country item's country of origin as country code (e.g. "de")
* @param category item category
* @param metadata additional information of the item
* @return matching questionnaire
*/
public Questionnaire build(String country, Category category)
public Questionnaire build(String country, Category category,
DDBItem metadata)
throws UnsupportedCountryException, UnsupportedCategoryException {

FlowChartState flowchart = getFlowchartForCountry(country);
FlowChartState initial = getInitialStateForCategory(flowchart, category);
return new Questionnaire(initial);
return new Questionnaire(initial, metadata);
}

private FlowChartState getFlowchartForCountry(String country) {
Expand Down
69 changes: 50 additions & 19 deletions src/main/java/de/ddb/pdc/storage/MongoStorageService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package de.ddb.pdc.storage;

import com.mongodb.MongoClient;
import de.ddb.pdc.core.PDCResult;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -26,9 +30,15 @@ public class MongoStorageService implements StorageService {
private final String collectionName;

@Autowired
public MongoStorageService(MongoTemplate mongoTemplate,
@Value("${collection.name:pdcData}") String collectionName) {
this.mongoTemplate = mongoTemplate;
public MongoStorageService(@Value("${host.ip:127.0.0.1}") String hostIP,
@Value("${host.port:27017}") int hostPort,
@Value("${database:pdc}") String database,
@Value("${collection.name:pdcData}") String collectionName)
throws UnknownHostException {

this.mongoTemplate = new MongoTemplate(
new MongoClient(hostIP, hostPort), database
);
this.collectionName = collectionName;
}

Expand All @@ -37,44 +47,65 @@ public MongoStorageService(MongoTemplate mongoTemplate,
*
*/
@Override
public void store(StoredPDCResult record) {
mongoTemplate.insert(record, collectionName);
public void store(PDCResult record) {
if (record != null) {
StoredPDCResult storageRecord = StorageUtils.toStoredPDCResult(record);
mongoTemplate.insert(storageRecord, collectionName);
}
}

/**
* Updates a record by removing the existing record and calling the
* {@link store(StorageModel)} method to store the new record.
* {@link store(PDCResult)} method to store the new record.
* The record that is removed is the first record that matches the itemId.
*
*/
@Override
public void update(StoredPDCResult newRecord) {
Query query = new Query();
query.addCriteria(Criteria.where("itemId").is(newRecord.getItemId()));
mongoTemplate.remove(query, collectionName);
this.store(newRecord);
public void update(PDCResult newRecord) {
if (newRecord != null) {
Query query = new Query();
query.addCriteria(Criteria.where("itemId").is(newRecord.getItemId()));
mongoTemplate.remove(query, collectionName);
this.store(newRecord);
}
}

/**
* Fetches the first StorageModel record from the collection that matches
* the query. Implements the Query class so SQL-like constructs can be used.
* Fetches the first record from the collection that matches the query.
*
* Implements the Query class so SQL-like constructs can be used.
* @return the target record or null if the record was not found.
*
*/
@Override
public StoredPDCResult fetch(String itemId) {
public PDCResult fetch(String itemId) {
Query query = new Query();
query.addCriteria(Criteria.where("itemId").is(itemId));
return mongoTemplate.findOne(query, StoredPDCResult.class, collectionName);
StoredPDCResult fetchedRecord = mongoTemplate.findOne(
query, StoredPDCResult.class, collectionName
);
if (fetchedRecord == null) {
return null;
} else {
return new PDCResult(fetchedRecord);
}
}

/**
* Fetch all MongoDataModel records from the collection.
*
* Fetch all records from the collection.
* A linked list is used to store the converted storage records.
* @return list of MongoDataModel records
*/
@Override
public List<StoredPDCResult> fetchAll() {
return mongoTemplate.findAll(StoredPDCResult.class, collectionName);
public List<PDCResult> fetchAll() {
List<StoredPDCResult> fetchedStorageRecords = mongoTemplate.findAll(
StoredPDCResult.class, collectionName
);
List<PDCResult> fetchedRecords = new LinkedList();
for (StoredPDCResult storageRecord : fetchedStorageRecords) {
fetchedRecords.add(new PDCResult(storageRecord));
}
return fetchedRecords;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/de/ddb/pdc/storage/StorageService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.ddb.pdc.storage;

import de.ddb.pdc.core.PDCResult;
import java.util.List;

/**
Expand All @@ -12,29 +13,29 @@ public interface StorageService {
*
* @param record
*/
public void store(StoredPDCResult record);
public void store(PDCResult record);

/**
* Update an existing MongoDataModel.
*
* @param record
*/
public void update(StoredPDCResult record);
public void update(PDCResult record);

/**
* Find a single MongoDataModel record by its item ID.
*
* @param itemId
* @return record
*/
public StoredPDCResult fetch(String itemId);
public PDCResult fetch(String itemId);

/**
* Fetch all records from storage.
*
* @return list of records
*/
public List<StoredPDCResult> fetchAll();
public List<PDCResult> fetchAll();

/**
* Remove all records from storage.
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/de/ddb/pdc/storage/StorageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.ddb.pdc.storage;

import de.ddb.pdc.core.PDCResult;

/**
* Helper class for storage-related conversions.
*/
public class StorageUtils {

public static StoredPDCResult toStoredPDCResult(PDCResult pdcResult) {

This comment has been minimized.

Copy link
@denisw

denisw Jan 3, 2015

Contributor

Instead of adding one of those utility classes, it would be nicer to add a StoredPDCResult(PDCResult) constructor or - if this is isn't possible - a static method in StoredPDCResult such as StoredPDCResult.fromPDCResult(PDCResult).

This comment has been minimized.

Copy link
@phil-fuber

phil-fuber Jan 5, 2015

Contributor

i did the latter, as the first suggestion also threw a mapping exception.

return new StoredPDCResult(
pdcResult.getItemId(),
pdcResult.getItemCategory(),
pdcResult.getInstitution(),
pdcResult.isPublicDomain(),
pdcResult.getTrace(),
pdcResult.getCreatedDate()
);
}

}
Loading

1 comment on commit f28caaf

@phil-fuber
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still need more tests for the PDCController to cover everything but i ran some checks manually and it was fine.

Please sign in to comment.