Search in sources :

Example 1 with ConnectFieldMeta

use of fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta in project muikku by otavanopisto.

the class FieldTranslator method translateConnectField.

public ConnectFieldMeta translateConnectField(String name, List<ConnectFieldOption> options) {
    List<ConnectFieldOptionMeta> connectFieldOptionMetas = new ArrayList<ConnectFieldOptionMeta>();
    List<ConnectFieldOptionMeta> counterparts = new ArrayList<ConnectFieldOptionMeta>();
    ;
    List<ConnectFieldConnectionMeta> connectFieldConnectionMetas = new ArrayList<>();
    for (int i = 0; i < options.size(); i++) {
        ConnectFieldOption option = options.get(i);
        String fieldName = String.valueOf(i + 1);
        String counterpartName = getExcelStyleLetterIndex(i);
        if (StringUtils.length(option.getTerm()) > 255) {
            option.setTerm(option.getTerm().substring(0, 254));
        }
        if (StringUtils.length(option.getEquivalent()) > 255) {
            option.setEquivalent(option.getEquivalent().substring(0, 254));
        }
        connectFieldOptionMetas.add(new ConnectFieldOptionMeta(fieldName, option.getTerm()));
        counterparts.add(new ConnectFieldOptionMeta(counterpartName, option.getEquivalent()));
        connectFieldConnectionMetas.add(new ConnectFieldConnectionMeta(fieldName, option.getAnswer()));
    }
    return new ConnectFieldMeta(name, connectFieldOptionMetas, counterparts, connectFieldConnectionMetas);
}
Also used : ConnectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta) ConnectFieldConnectionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldConnectionMeta) ArrayList(java.util.ArrayList) ConnectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldOptionMeta) ConnectFieldOption(fi.otavanopisto.muikku.plugins.dnm.parser.content.ConnectFieldOption)

Example 2 with ConnectFieldMeta

use of fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta in project muikku by otavanopisto.

the class FieldElementsHandler method handleConnectField.

@Override
public Node handleConnectField(org.w3c.dom.Document ownerDocument, String paramName, List<ConnectFieldOption> options, String help, String hint) {
    ConnectFieldMeta connectFieldData = fieldTranslator.translateConnectField(paramName, options);
    Element outerSpan = ownerDocument.createElement("span");
    outerSpan.setAttribute("style", "display: table;");
    int fieldCount = 0;
    for (ConnectFieldOption connectFieldOption : options) {
        Element rowSpan = ownerDocument.createElement("span");
        rowSpan.setAttribute("style", "display: table-row;");
        Element leftSpan = ownerDocument.createElement("span");
        leftSpan.setAttribute("style", "display: table-cell;");
        Element centerSpan = ownerDocument.createElement("span");
        centerSpan.setAttribute("style", "display: table-cell;");
        Element rightSpan = ownerDocument.createElement("span");
        rightSpan.setAttribute("style", "display: table-cell;");
        Element input = ownerDocument.createElement("input");
        input.setAttribute("type", "text");
        input.setAttribute("name", paramName);
        input.setAttribute("data-fieldcount", String.valueOf(fieldCount));
        leftSpan.setTextContent(connectFieldOption.getTerm());
        centerSpan.appendChild(input);
        rightSpan.setTextContent(connectFieldOption.getEquivalent());
        rowSpan.appendChild(leftSpan);
        rowSpan.appendChild(centerSpan);
        rowSpan.appendChild(rightSpan);
        outerSpan.appendChild(rowSpan);
        fieldCount++;
    }
    return wrapWithObjectElement(ownerDocument, outerSpan, connectFieldData);
}
Also used : ConnectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta) Element(org.w3c.dom.Element) ConnectFieldOption(fi.otavanopisto.muikku.plugins.dnm.parser.content.ConnectFieldOption)

Example 3 with ConnectFieldMeta

use of fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta 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()));
        }
    }
}
Also used : ConnectFieldMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta) HashMap(java.util.HashMap) QueryConnectFieldCounterpart(fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldCounterpart) ConnectFieldConnectionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldConnectionMeta) IOException(java.io.IOException) QueryConnectFieldTerm(fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldTerm) ConnectFieldOptionMeta(fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldOptionMeta) QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) QueryConnectField(fi.otavanopisto.muikku.plugins.material.model.QueryConnectField)

Aggregations

ConnectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta)3 ConnectFieldOption (fi.otavanopisto.muikku.plugins.dnm.parser.content.ConnectFieldOption)2 ConnectFieldConnectionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldConnectionMeta)2 ConnectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldOptionMeta)2 QueryConnectField (fi.otavanopisto.muikku.plugins.material.model.QueryConnectField)1 QueryConnectFieldCounterpart (fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldCounterpart)1 QueryConnectFieldTerm (fi.otavanopisto.muikku.plugins.material.model.QueryConnectFieldTerm)1 QueryField (fi.otavanopisto.muikku.plugins.material.model.QueryField)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 Element (org.w3c.dom.Element)1