use of com.entwinemedia.fn.Fn in project opencast by opencast.
the class PersistenceEnvs method mk.
/**
* Create a new, concurrently usable persistence environment which uses JPA local transactions.
* <p>
* Transaction propagation is supported on a per thread basis.
*/
public static PersistenceEnv mk(final EntityManagerFactory emf) {
final Transactional startTx = new Transactional() {
@Override
public <A> A tx(Fn<EntityManager, A> transactional) {
for (final EntityManager em : mkEntityManager(emf)) {
final EntityTransaction tx = em.getTransaction();
try {
tx.begin();
emStore.set(Opt.<Transactional>some(new Transactional() {
@Override
public <A> A tx(Fn<EntityManager, A> transactional) {
return transactional.apply(em);
}
}));
A ret = transactional.apply(em);
tx.commit();
return ret;
} catch (Exception e) {
if (tx.isActive()) {
tx.rollback();
}
// propagate exception
return chuck(e);
} finally {
if (em.isOpen())
em.close();
emStore.remove();
}
}
return chuck(new IllegalStateException("EntityManager is already closed"));
}
};
return new PersistenceEnv() {
Transactional currentTx() {
return emStore.get().getOr(startTx);
}
@Override
public <A> A tx(Fn<EntityManager, A> transactional) {
return currentTx().tx(transactional);
}
@Override
public void close() {
emf.close();
}
};
}
use of com.entwinemedia.fn.Fn in project opencast by opencast.
the class MetadataField method createIterableStringMetadataField.
/**
* Create a metadata field of type iterable String
*
* @param inputID
* The identifier of the new metadata field
* @param label
* The label of the new metadata field
* @param readOnly
* Define if the new metadata field can be or not edited
* @param required
* Define if the new metadata field is or not required
* @param isTranslatable
* If the field value is not human readable and should be translated before
* @param collection
* If the field has a limited list of possible value, the option should contain this one. Otherwise it should
* be none.
* @param order
* The ui order for the new field, 0 at the top and progressively down from there.
* @return the new metadata field
*/
public static MetadataField<Iterable<String>> createIterableStringMetadataField(String inputID, Opt<String> outputID, String label, boolean readOnly, boolean required, Opt<Boolean> isTranslatable, Opt<Map<String, String>> collection, Opt<String> collectionId, Opt<Integer> order, Opt<String> namespace) {
Fn<Opt<Iterable<String>>, JValue> iterableToJSON = new Fn<Opt<Iterable<String>>, JValue>() {
@Override
public JValue apply(Opt<Iterable<String>> value) {
if (value.isNone())
return arr();
Object val = value.get();
List<JValue> list = new ArrayList<>();
if (val instanceof String) {
// The value is a string so we need to split it.
String stringVal = (String) val;
for (String entry : stringVal.split(",")) {
list.add(v(entry, Jsons.BLANK));
}
} else {
// The current value is just an iterable string.
for (Object v : value.get()) {
list.add(v(v, Jsons.BLANK));
}
}
return arr(list);
}
};
Fn<Object, Iterable<String>> jsonToIterable = new Fn<Object, Iterable<String>>() {
@Override
public Iterable<String> apply(Object arrayIn) {
JSONArray array = (JSONArray) arrayIn;
if (array == null)
return null;
String[] arrayOut = new String[array.size()];
for (int i = 0; i < array.size(); i++) {
arrayOut[i] = (String) array.get(i);
}
return Arrays.asList(arrayOut);
}
};
return new MetadataField<>(inputID, outputID, label, readOnly, required, new ArrayList<String>(), isTranslatable, Type.ITERABLE_TEXT, JsonType.TEXT, collection, collectionId, iterableToJSON, jsonToIterable, order, namespace);
}
use of com.entwinemedia.fn.Fn in project opencast by opencast.
the class MetadataField method createMixedIterableStringMetadataField.
/**
* Create a metadata field of type mixed iterable String
*
* @param inputID
* The identifier of the new metadata field
* @param label
* The label of the new metadata field
* @param readOnly
* Define if the new metadata field can be or not edited
* @param required
* Define if the new metadata field is or not required
* @param isTranslatable
* If the field value is not human readable and should be translated before
* @param collection
* If the field has a limited list of possible value, the option should contain this one. Otherwise it should
* be none.
* @param order
* The ui order for the new field, 0 at the top and progressively down from there.
* @return the new metadata field
*/
public static MetadataField<Iterable<String>> createMixedIterableStringMetadataField(String inputID, Opt<String> outputID, String label, boolean readOnly, boolean required, Opt<Boolean> isTranslatable, Opt<Map<String, String>> collection, Opt<String> collectionId, Opt<Integer> order, Opt<String> namespace) {
Fn<Opt<Iterable<String>>, JValue> iterableToJSON = new Fn<Opt<Iterable<String>>, JValue>() {
@Override
public JValue apply(Opt<Iterable<String>> value) {
if (value.isNone())
return arr();
Object val = value.get();
List<JValue> list = new ArrayList<>();
if (val instanceof String) {
// The value is a string so we need to split it.
String stringVal = (String) val;
for (String entry : stringVal.split(",")) {
if (StringUtils.isNotBlank(entry))
list.add(v(entry, Jsons.BLANK));
}
} else {
// The current value is just an iterable string.
for (Object v : value.get()) {
list.add(v(v, Jsons.BLANK));
}
}
return arr(list);
}
};
Fn<Object, Iterable<String>> jsonToIterable = new Fn<Object, Iterable<String>>() {
@Override
public Iterable<String> apply(Object arrayIn) {
JSONParser parser = new JSONParser();
JSONArray array;
if (arrayIn instanceof String) {
try {
array = (JSONArray) parser.parse((String) arrayIn);
} catch (ParseException e) {
throw new IllegalArgumentException("Unable to parse Mixed Iterable value into a JSONArray:", e);
}
} else {
array = (JSONArray) arrayIn;
}
if (array == null)
return new ArrayList<>();
String[] arrayOut = new String[array.size()];
for (int i = 0; i < array.size(); i++) {
arrayOut[i] = (String) array.get(i);
}
return Arrays.asList(arrayOut);
}
};
return new MetadataField<>(inputID, outputID, label, readOnly, required, new ArrayList<String>(), isTranslatable, Type.MIXED_TEXT, JsonType.MIXED_TEXT, collection, collectionId, iterableToJSON, jsonToIterable, order, namespace);
}
use of com.entwinemedia.fn.Fn in project opencast by opencast.
the class ToolsEndpoint method createSmilCuttingCatalog.
/**
* Creates a SMIL cutting catalog based on the passed editing information and the media package.
*
* @param editingInfo
* the editing information
* @param mediaPackage
* the media package
* @return a SMIL catalog
* @throws SmilException
* if creating the SMIL catalog failed
*/
Smil createSmilCuttingCatalog(final EditingInfo editingInfo, final MediaPackage mediaPackage) throws SmilException {
// Create initial SMIL catalog
SmilResponse smilResponse = smilService.createNewSmil(mediaPackage);
// Add tracks to the SMIL catalog
ArrayList<Track> tracks = new ArrayList<>();
for (final String trackId : editingInfo.getConcatTracks()) {
Track track = mediaPackage.getTrack(trackId);
if (track == null) {
Opt<Track> trackOpt = getInternalPublication(mediaPackage).toStream().bind(new Fn<Publication, List<Track>>() {
@Override
public List<Track> apply(Publication a) {
return Arrays.asList(a.getTracks());
}
}).filter(new Fn<Track, Boolean>() {
@Override
public Boolean apply(Track a) {
return trackId.equals(a.getIdentifier());
}
}).head();
if (trackOpt.isNone())
throw new IllegalStateException(format("The track '%s' doesn't exist in media package '%s'", trackId, mediaPackage));
track = trackOpt.get();
}
tracks.add(track);
}
for (Tuple<Long, Long> segment : editingInfo.getConcatSegments()) {
smilResponse = smilService.addParallel(smilResponse.getSmil());
final String parentId = smilResponse.getEntity().getId();
final Long duration = segment.getB() - segment.getA();
smilResponse = smilService.addClips(smilResponse.getSmil(), parentId, tracks.toArray(new Track[tracks.size()]), segment.getA(), duration);
}
return smilResponse.getSmil();
}
use of com.entwinemedia.fn.Fn in project opencast by opencast.
the class AccessControlUtil method calculateChecksum.
/**
* Calculate an MD5 checksum for an {@link AccessControlList}.
*/
public static Checksum calculateChecksum(AccessControlList acl) {
// Use 0 as a word separator. This is safe since none of the UTF-8 code points
// except \u0000 contains a null byte when converting to a byte array.
final byte[] sep = new byte[] { 0 };
final MessageDigest md = $(acl.getEntries()).sort(sortAcl).bind(new Fn<AccessControlEntry, Stream<String>>() {
@Override
public Stream<String> apply(AccessControlEntry entry) {
return $(entry.getRole(), entry.getAction(), Boolean.toString(entry.isAllow()));
}
}).foldl(mkMd5MessageDigest(), new Fn2<MessageDigest, String, MessageDigest>() {
@Override
public MessageDigest apply(MessageDigest digest, String s) {
digest.update(s.getBytes(StandardCharsets.UTF_8));
// add separator byte (see definition above)
digest.update(sep);
return digest;
}
});
try {
return Checksum.create("md5", Checksum.convertToHex(md.digest()));
} catch (NoSuchAlgorithmException e) {
return chuck(e);
}
}
Aggregations