|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package org.olat.modules.qpool.manager; |
|
|
|
import org.olat.core.util.StringHelper; |
|
import org.olat.core.util.xml.XStreamHelper; |
|
import org.olat.modules.qpool.QuestionItem; |
|
import org.olat.modules.qpool.model.LOMDuration; |
|
import org.olat.modules.qpool.model.QEducationalContext; |
|
import org.olat.modules.qpool.model.QItemType; |
|
import org.olat.modules.qpool.model.QLicense; |
|
import org.olat.modules.qpool.model.QuestionItemImpl; |
|
import org.olat.modules.taxonomy.model.TaxonomyLevelImpl; |
|
|
|
import com.thoughtworks.xstream.XStream; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class MetadataConverterHelper { |
|
|
|
private static XStream metadatXstream = XStreamHelper.createXStreamInstance(); |
|
static { |
|
XStreamHelper.allowDefaultPackage(metadatXstream); |
|
metadatXstream.alias("item", QuestionItemImpl.class); |
|
metadatXstream.alias("educationalContext", QEducationalContext.class); |
|
metadatXstream.alias("itemType", QItemType.class); |
|
metadatXstream.alias("license", QLicense.class); |
|
metadatXstream.alias("taxonomyLevel", TaxonomyLevelImpl.class); |
|
} |
|
|
|
public static String toXml(QuestionItem item) { |
|
return metadatXstream.toXML(item); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String convertDuration(int day, int hour, int minute, int seconds) { |
|
StringBuilder sb = new StringBuilder(); |
|
boolean hasD = day > 0; |
|
boolean hasT = (hour > 0 || minute > 0 || seconds > 0); |
|
if(hasD || hasT) { |
|
sb.append("P"); |
|
if(hasD) { |
|
sb.append(day).append("D"); |
|
} |
|
if(hasT) { |
|
sb.append("T"); |
|
if(hour > 0) { |
|
sb.append(hour).append("H"); |
|
} |
|
if(minute > 0) { |
|
sb.append(minute).append("M"); |
|
} |
|
if(seconds > 0) { |
|
sb.append(seconds).append("S"); |
|
} |
|
} |
|
} |
|
return sb.toString(); |
|
} |
|
|
|
public static LOMDuration convertDuration(String durationStr) { |
|
LOMDuration duration = new LOMDuration(); |
|
if(StringHelper.containsNonWhitespace(durationStr) && durationStr.startsWith("P")) { |
|
|
|
durationStr = durationStr.substring(1, durationStr.length()); |
|
int indexT = durationStr.indexOf('T'); |
|
if(indexT < 0) { |
|
convertDurationP(durationStr, duration); |
|
} else { |
|
String pDurationStr = durationStr.substring(0, indexT); |
|
convertDurationP(pDurationStr, duration); |
|
String tDurationStr = durationStr.substring(indexT + 1, durationStr.length()); |
|
convertDurationT(tDurationStr, duration); |
|
} |
|
} |
|
return duration; |
|
} |
|
|
|
private static void convertDurationP(String durationStr, LOMDuration duration) { |
|
int indexY = durationStr.indexOf('Y'); |
|
if(indexY >= 0) { |
|
duration.setYear(extractValueFromDuration(durationStr, indexY)); |
|
durationStr = durationStr.substring(indexY + 1, durationStr.length()); |
|
} |
|
int indexM = durationStr.indexOf('M'); |
|
if(indexM >= 0) { |
|
duration.setMonth(extractValueFromDuration(durationStr, indexM)); |
|
durationStr = durationStr.substring(indexM + 1, durationStr.length()); |
|
} |
|
int indexD = durationStr.indexOf('D'); |
|
if(indexD >= 0) { |
|
duration.setDay(extractValueFromDuration(durationStr, indexD)); |
|
durationStr = durationStr.substring(indexD + 1, durationStr.length()); |
|
} |
|
} |
|
|
|
public static long convertToSeconds(LOMDuration duration) { |
|
long time = duration.getSeconds(); |
|
time += (duration.getMinute() * 60); |
|
time += (duration.getHour() * (60 * 60)); |
|
time += (duration.getDay() * (60 * 60 * 24)); |
|
time += (duration.getMonth() * (60 * 60 * 24 * 30)); |
|
time += (duration.getYear() * (60 * 60 * 24 * 30 * 365)); |
|
return time; |
|
} |
|
|
|
private static void convertDurationT(String durationStr, LOMDuration duration) { |
|
int indexH = durationStr.indexOf('H'); |
|
if(indexH >= 0) { |
|
duration.setHour(extractValueFromDuration(durationStr, indexH)); |
|
durationStr = durationStr.substring(indexH + 1, durationStr.length()); |
|
} |
|
int indexMin = durationStr.indexOf('M'); |
|
if(indexMin >= 0) { |
|
duration.setMinute(extractValueFromDuration(durationStr, indexMin)); |
|
durationStr = durationStr.substring(indexMin + 1, durationStr.length()); |
|
} |
|
int indexS = durationStr.indexOf('S'); |
|
if(indexS >= 0) { |
|
duration.setSeconds(extractValueFromDuration(durationStr, indexS)); |
|
durationStr = durationStr.substring(indexS + 1, durationStr.length()); |
|
} |
|
} |
|
|
|
private static int extractValueFromDuration(String durationStr, int index) |
|
throws NumberFormatException { |
|
if(index <= 0) return 0; |
|
String intVal = durationStr.substring(0, index); |
|
return Integer.parseInt(intVal); |
|
} |
|
} |
|
|