use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.
the class WorkspaceConnectFieldIOHandler method retrieve.
@Override
public String retrieve(WorkspaceMaterialField field, WorkspaceMaterialReply reply) throws WorkspaceFieldIOException {
Map<String, String> answers = new HashMap<>();
QueryConnectField queryConnectField = (QueryConnectField) field.getQueryField();
List<QueryConnectFieldTerm> terms = queryConnectFieldController.listConnectFieldTermsByField(queryConnectField);
for (QueryConnectFieldTerm term : terms) {
WorkspaceMaterialConnectFieldAnswer connectFieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialConnectFieldAnswerByFieldAndReplyAndTerm(field, reply, term);
if ((connectFieldAnswer != null) && (connectFieldAnswer.getCounterpart() != null)) {
answers.put(term.getName(), connectFieldAnswer.getCounterpart().getName());
}
}
try {
return new ObjectMapper().writeValueAsString(answers);
} catch (IOException e) {
throw new WorkspaceFieldIOException("Could not marshal field response", e);
}
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.
the class HtmlMaterialFieldChangeListener method onHtmlMaterialConnectFieldCreated.
// Connect field
public void onHtmlMaterialConnectFieldCreated(@Observes HtmlMaterialFieldCreateEvent event) throws MaterialQueryIntegrityExeption, MaterialFieldMetaParsingExeption {
if (event.getField().getType().equals("application/vnd.muikku.field.connect")) {
ObjectMapper objectMapper = new ObjectMapper();
ConnectFieldMeta connectFieldMeta;
try {
connectFieldMeta = objectMapper.readValue(event.getField().getContent(), ConnectFieldMeta.class);
} catch (IOException e) {
throw new MaterialFieldMetaParsingExeption("Could not parse connect field meta", e);
}
QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(event.getMaterial(), connectFieldMeta.getName());
if (queryField != null) {
throw new MaterialQueryIntegrityExeption("Field with same name already exists in the database");
}
QueryConnectField field = queryConnectFieldController.createQueryConnectField(event.getMaterial(), connectFieldMeta.getName());
Map<String, QueryConnectFieldTerm> terms = new HashMap<String, QueryConnectFieldTerm>();
Map<String, QueryConnectFieldCounterpart> counterparts = new HashMap<String, QueryConnectFieldCounterpart>();
for (ConnectFieldOptionMeta option : connectFieldMeta.getFields()) {
terms.put(option.getName(), queryConnectFieldController.createConnectFieldTerm(field, option.getName(), option.getText(), null));
}
for (ConnectFieldOptionMeta option : connectFieldMeta.getCounterparts()) {
counterparts.put(option.getName(), queryConnectFieldController.createConnectFieldCounterpart(field, option.getName(), option.getText()));
}
for (ConnectFieldConnectionMeta connection : connectFieldMeta.getConnections()) {
terms.get(connection.getField()).setCounterpart(counterparts.get(connection.getCounterpart()));
}
}
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.
the class QueryConnectFieldDAO method create.
public QueryConnectField create(Material material, String name) {
QueryConnectField queryConnectField = new QueryConnectField();
queryConnectField.setMaterial(material);
queryConnectField.setName(name);
return persist(queryConnectField);
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.
the class QueryConnectFieldDAO method findByMaterialAndName.
public QueryConnectField findByMaterialAndName(Material material, String name) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<QueryConnectField> criteria = criteriaBuilder.createQuery(QueryConnectField.class);
Root<QueryConnectField> root = criteria.from(QueryConnectField.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(QueryConnectField_.material), material), criteriaBuilder.equal(root.get(QueryConnectField_.name), name)));
return getSingleResult(entityManager.createQuery(criteria));
}
use of fi.otavanopisto.muikku.plugins.material.model.QueryConnectField in project muikku by otavanopisto.
the class WorkspaceConnectFieldIOHandler method store.
@Override
public void store(WorkspaceMaterialField field, WorkspaceMaterialReply reply, String value) throws WorkspaceFieldIOException {
Map<String, String> valueMap = null;
try {
valueMap = new ObjectMapper().readValue(value, new TypeReference<Map<String, String>>() {
});
} catch (IOException e) {
throw new WorkspaceFieldIOException("Could not unmarshal field response", e);
}
QueryConnectField queryConnectField = (QueryConnectField) field.getQueryField();
if (queryConnectField != null) {
List<QueryConnectFieldTerm> terms = queryConnectFieldController.listConnectFieldTermsByField(queryConnectField);
for (QueryConnectFieldTerm term : terms) {
String parameterValue = valueMap.get(term.getName());
QueryConnectFieldCounterpart counterpart = StringUtils.isNotEmpty(parameterValue) ? queryConnectFieldController.findQueryConnectFieldCounterpartByFieldAndName(queryConnectField, parameterValue) : null;
WorkspaceMaterialConnectFieldAnswer connectFieldAnswer = workspaceMaterialFieldAnswerController.findWorkspaceMaterialConnectFieldAnswerByFieldAndReplyAndTerm(field, reply, term);
if (connectFieldAnswer != null) {
workspaceMaterialFieldAnswerController.updateWorkspaceMaterialConnectFieldAnswerCounterpart(connectFieldAnswer, counterpart);
} else {
if (counterpart != null) {
workspaceMaterialFieldAnswerController.createWorkspaceMaterialConnectFieldAnswer(field, reply, term, counterpart);
}
}
}
} else {
throw new WorkspaceFieldIOException("Workspace material connect field #" + field.getId() + " points to non-existing field ");
}
}
Aggregations