//---------------------------------------------------------------------------
// function builds a OAI records from a metadata record, according to the arguments select and selectVal
public static Record buildRecordStat(ServiceContext context, Specification<Metadata> spec/*String select, Object selectVal*/,
String prefix) throws Exception {
GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
SchemaManager sm = gc.getBean(SchemaManager.class);
Metadata metadata = context.getBean(MetadataRepository.class).findOne(spec);
if (metadata == null)
throw new IdDoesNotExistException(spec.toString());
String uuid = metadata.getUuid();
final MetadataDataInfo dataInfo = metadata.getDataInfo();
String schema = dataInfo.getSchemaId();
String changeDate = dataInfo.getChangeDate().getDateAndTime();
String data = metadata.getData();
Element md = Xml.loadString(data, false);
//--- try to disseminate format
if (prefix.equals(schema)) {
Attribute schemaLocAtt = sm.getSchemaLocation(schema, context);
if (schemaLocAtt != null) {
if (md.getAttribute(schemaLocAtt.getName(), schemaLocAtt.getNamespace()) == null) {
md.setAttribute(schemaLocAtt);
// make sure namespace declaration for schemalocation is present -
// remove it first (does nothing if not there) then add it
md.removeNamespaceDeclaration(schemaLocAtt.getNamespace());
md.addNamespaceDeclaration(schemaLocAtt.getNamespace());
}
}
} else {
String schemaDir = sm.getSchemaDir(schema);
if (Lib.existsConverter(schemaDir, prefix)) {
final String siteURL = context.getBean(SettingManager.class).getSiteURL(context);
Element env = Lib.prepareTransformEnv(uuid, changeDate, context.getBaseUrl(), siteURL, gc.getBean(SettingManager.class)
.getSiteName());
md = Lib.transform(schemaDir, env, md, prefix + ".xsl");
} else {
throw new CannotDisseminateFormatException("Unknown prefix : " + prefix);
}