Search in sources :

Example 1 with ConnectFieldOptionMeta

use of fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldOptionMeta 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 ConnectFieldOptionMeta

use of fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldOptionMeta 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

ConnectFieldConnectionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldConnectionMeta)2 ConnectFieldMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldMeta)2 ConnectFieldOptionMeta (fi.otavanopisto.muikku.plugins.material.fieldmeta.ConnectFieldOptionMeta)2 ConnectFieldOption (fi.otavanopisto.muikku.plugins.dnm.parser.content.ConnectFieldOption)1 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