Project

General

Profile

1
package eu.dnetlib.data.transform.xml;
2

    
3
import com.google.common.collect.Lists;
4
import com.google.protobuf.Descriptors;
5
import eu.dnetlib.data.proto.*;
6
import eu.dnetlib.data.proto.WdsPublicationProtos;
7
import org.apache.commons.lang3.StringUtils;
8
import org.apache.commons.lang3.exception.ExceptionUtils;
9
import org.w3c.dom.Node;
10
import org.w3c.dom.NodeList;
11

    
12
import java.util.List;
13

    
14
public class WDSPmfToHbaseXsltFunctions extends OafToHbaseXsltFunctions {
15

    
16

    
17
    public static String pmfPublication(
18
            final String resultId,
19
            final String provenance,
20
            final String trust,
21
            final NodeList about,
22
            final String originalId,
23
            final String dateOfCollection,
24
            final String dateOfTransformation,
25
            final NodeList metadataNodes) {
26
        try {
27
            final ValueMap values = ValueMap.parseNodeList(metadataNodes);
28
            final Descriptors.Descriptor mDesc = PublicationProtos.Publication.Metadata.getDescriptor();
29
            final DNGFProtos.DNGFEntity.Builder entity = DNGFProtos.DNGFEntity.newBuilder();
30

    
31
            
32
            final PublicationProtos.Publication.Metadata.Builder metadata = buildMetadata(values, mDesc);
33
            addField(metadata, mDesc.findFieldByName("dateofacceptance"), values.get("date").listValues());
34
            final PublicationProtos.Publication.Builder publication = buildPublication(metadata, values, mDesc, "", "");
35
            parseProject(publication.getMetadataBuilder(), metadataNodes);
36
            final List<FieldTypeProtos.StructuredProperty> pids = Lists.newArrayList();
37
            pids.addAll(parsePids(metadataNodes));
38
            entity.setType(TypeProtos.Type.publication).setId(resultId);
39
            entity.setDateoftransformation(StringUtils.isBlank(dateOfTransformation) ? "" : dateOfTransformation);
40
            entity.setDateofcollection(StringUtils.isBlank(dateOfCollection) ? "" : dateOfCollection);
41
            entity.addAllPid(pids);
42
            entity.setDateofcollection(dateOfCollection)
43
                    .setDateoftransformation(dateOfTransformation).setOaiprovenance(getOAIProvenance(about));
44
            entity.setPublication(publication);
45

    
46
            final DNGFProtos.DNGF oaf = getOaf(entity, getDataInfo(about, provenance, trust, false, false));
47
            return base64(oaf.toByteArray());
48
        } catch (final Throwable e) {
49
            System.err.println(ExceptionUtils.getStackTrace(e));
50
            throw new IllegalArgumentException(e);
51
        }
52
    }
53

    
54
    private static  void parseProject(final PublicationProtos.Publication.Metadata.Builder metadata, final  NodeList metadataNodes ) {
55
        for (int i = 0; i < metadataNodes.getLength(); i++) {
56
            final Node node = metadataNodes.item(i);
57
            String acronym = null;
58
            String funder = null;
59
            String fundingStream = null;
60
            String grantId = null;
61
            String name = null;
62
            if (node.getNodeType() == Node.ELEMENT_NODE) {
63
                if (node.getLocalName().equalsIgnoreCase("project")) {
64
                    acronym = node.getAttributes().getNamedItem("acronym").getTextContent();
65
                    funder = node.getAttributes().getNamedItem("funder").getTextContent();
66
                    fundingStream = node.getAttributes().getNamedItem("fundingStream").getTextContent();
67
                    grantId = node.getAttributes().getNamedItem("grantId").getTextContent();
68
                    name = node.getTextContent();
69
                    metadata.addExtension(WdsPublicationProtos.WdsPublication.projects,
70
                            FieldTypeProtos.ProjectRelation.newBuilder()
71
                                    .setAcronym(acronym)
72
                                    .setFunder(funder)
73
                                    .setFundingStream(fundingStream)
74
                                    .setGrantId(grantId)
75
                                    .setName(name)
76
                            .build());
77

    
78

    
79

    
80

    
81
                }
82

    
83
            }
84
        }
85

    
86
    }
87

    
88

    
89
    protected static List<FieldTypeProtos.StructuredProperty> parsePids(final NodeList nodelist) {
90

    
91
        final List<FieldTypeProtos.StructuredProperty> pids = Lists.newArrayList();
92

    
93
        for (int i = 0; i < nodelist.getLength(); i++) {
94
            final Node node = nodelist.item(i);
95
            String pidType = null;
96
            String pidValue = null;
97
            if (node.getNodeType() == Node.ELEMENT_NODE) {
98
                if (node.getLocalName().equalsIgnoreCase("pid")) {
99
                    pidType = node.getAttributes().getNamedItem("type").getTextContent();
100
                    pidValue = node.getTextContent();
101
                    if (StringUtils.isNotEmpty(pidType) && StringUtils.isNotEmpty(pidValue))
102
                    {
103
                        pids.add(getStructuredProperty(pidValue, pidType, pidType, "dnet:pid_types", "dnet:pid_types"));
104
                    }
105
                }
106
                //this is to handle dataset pids
107
            }
108
        }
109
        return pids;
110
    }
111
}
(2-2/3)