if (originalUrls[i] != null) {
try {
originalUrls[i] = new URL(baseURL, originalUrls[i])
.toString();
} catch (MalformedURLException e) {
throw new HandlerException(e);
}
}
}
Collection originalUrlCol = Arrays.asList(originalUrls);
Map originalLabelsMap = new HashMap();
// this contains langugage specific attachments in other languages
// as value and their poisition-Integer as key.
Map untouchedAttachments = new HashMap();
Statement attachStmt = target.getProperty(ATTACH.attachments);
if (attachStmt != null) {
Seq attachSeq = attachStmt.getSeq();
for (int i = 1; i <= attachSeq.size(); i++) {
Resource currentAttachment;
try {
currentAttachment = attachSeq.getResource(i);
} catch (PropertyNotFoundException ex) {
log.warn("Inncosistent sequence of attachment");
fixSeq(attachSeq);
i--;
continue;
}
Statement languageStmt = currentAttachment
.getProperty(DC.language);
if ((languageStmt == null)
|| (languageStmt.getString().equals(language))) {
String currentDocumentURL = currentAttachment
.getProperty(ATTACH.document).getResource()
.getURI();
if (originalUrlCol.contains(currentDocumentURL)) {
StmtIterator labelStmts = currentAttachment
.listProperties(RDFS.label);
Collection otherLangLabels = new ArrayList();
while (labelStmts.hasNext()) {
Statement current = labelStmts.nextStatement();
Literal value = current.getLiteral();
if (!language.equals(value.getLanguage())) {
otherLangLabels.add(value);
}
}
originalLabelsMap
.put(
currentDocumentURL,
otherLangLabels
.toArray(new Literal[otherLangLabels
.size()]));
// TODO put in map of attachments to update
}
currentAttachment.removeProperties();
} else {
// remove attachment from sequence anyway and re-insert
// it at the end at the best position
// best position: for the moment the same index if
// available in the new seq.
untouchedAttachments.put(new Integer(i),
currentAttachment);
}
attachSeq.remove(i--);
}
}
// for every attachment
String[] attachmentPositions = requestBody
.getParameterValues("attachment_position");
String[] attachmentTypes = requestBody
.getParameterValues("attachment_type");
String[] attachmentLabels = requestBody
.getParameterValues("attachment_label");
String[] attachmentWidths = requestBody
.getParameterValues("attachment_width");
String[] attachmentHeights = requestBody
.getParameterValues("attachment_height");
String[] attachmentMode = requestBody
.getParameterValues("attachment_mode");
String[] languageSpecific = requestBody
.getParameterValues("attachment_language_specific");
String[] summmaryViews = requestBody
.getParameterValues("attachment_summary_view");
// for url-attachments
String[] attachmentURLs = requestBody
.getParameterValues("attachment_url");
String[] urlAttachmentContentTypes = requestBody
.getParameterValues("attachment_contenttype");
// for upload-attachments
byte[][] attachmentContent = requestBody
.getFileContents("attachment_file");
String[] fileNames = requestBody.getFileNames("attachment_file");
MimeType[] fileContentTypes = requestBody
.getFileContentTypes("attachment_file");
// int urlIter = 0;
// int fileIter = 0;
SortedSet attachmentsSet = new TreeSet();
// Attachment[] attachments = new
// Attachment[attachmentTypes.length];
for (int i = 0; i < attachmentTypes.length; i++) {
int position = Integer.parseInt(attachmentPositions[i]);
if (position == -1) {
continue;
}
String currentType = attachmentTypes[i];
int width, height;
try {
width = Integer.parseInt(attachmentWidths[i]);
height = Integer.parseInt(attachmentHeights[i]);
} catch (NumberFormatException ex) {
width = -1;
height = -1;
}
Literal[] existingLabel = (Literal[]) originalLabelsMap
.get(originalUrls[i]);
Literal[] labels;
if (existingLabel != null) {
labels = new Literal[existingLabel.length + 1];
System.arraycopy(existingLabel, 0, labels, 0,
existingLabel.length);
} else {
labels = new Literal[1];
}
labels[labels.length - 1] = model.createLiteral(
attachmentLabels[i], language);
String attachmentLanguage;
if (languageSpecific[i].equals("true")) {
attachmentLanguage = language;
} else {
attachmentLanguage = null;
}
if ((currentType != null) && (currentType.equals("url"))) {
if ((attachmentURLs[i] != null)
&& (!attachmentURLs[i].equals(""))) {
URL attachmnetTargetURL;
try {
attachmnetTargetURL = new URL(baseURL,
attachmentURLs[i]);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
MimeType mimeType;
if ((urlAttachmentContentTypes[i] != null)
&& (!urlAttachmentContentTypes[i].equals(""))) {
try {
mimeType = new MimeType(
urlAttachmentContentTypes[i]);
} catch (MimeTypeParseException e) {
throw new HandlerException("Invalid MimeType",
e);
}
} else {
mimeType = null;
}
attachmentsSet.add(new Attachment(labels,
attachmnetTargetURL, mimeType, width, height,
attachmentMode[i].equals("inline"),
attachmentLanguage, position, summmaryViews[i]
.equals("true")));
/*
* Resource attachmentTarget =
* model.createResource(attachmnetTargetURL.toString());
* ItemUtil.addAttachment(target, attachmentTarget,
* urlAttachmentContentTypes[i], attachmentLabels[i],
* width, height, attachmentMode[i].equals("inline"));
*/
}
// urlIter++;
} else {
if ((attachmentContent != null)
&& (attachmentContent[i] != null)
&& (attachmentContent[i].length > 0)) {
String fileName = fileNames[i];
MimeType mimeType;
mimeType = fileContentTypes[i];
try {
attachmentsSet.add(new Attachment(model, hashStore,
new URL(target.getURI()), labels,
attachmentContent[i], fileName, mimeType,
width, height, attachmentMode[i]
.equals("inline"),
attachmentLanguage, position,
summmaryViews[i].equals("true"),
permissions));
/*
* ItemUtil.addAttachment(target,
* attachmentContent[i], fileNames[i],
* fileContentTypes[i], attachmentLabels[i], width,
* height, attachmentMode[i].equals("inline"));
*/
} catch (IOException e) {
throw new HandlerException(e);
}
}
// fileIter++;
}
}