DeviceRepository { public Device update(Device update, String log) throws DeviceException { String id = update.getId(); String label = update.getLabel(); String model = transformModel(update.getModel()); checkDeviceId(id); try { RemoteObject robj = remoteStorage.find(id); updateDc(robj, id, model, label, log); XmlStreamEditor descriptionEditor = getMixDescriptionEditor(robj); Source oldDescSrc = descriptionEditor.read(); if (oldDescSrc == null) { update.setTimestamp(descriptionEditor.getLastModified()); } if (oldDescSrc != null && update.getDescription() == null) { update.setDescription(new Mix()); } if (update.getDescription() != null) { EditorResult result = descriptionEditor.createResult(); MixUtils.marshal(result, update.getDescription(), true); descriptionEditor.write(result, update.getTimestamp(), log); } XmlStreamEditor audiodescriptionEditor = getPremisDescriptionEditor(robj); Source oldAudioDescSrc = audiodescriptionEditor.read(); if (oldAudioDescSrc == null) { update.setAudioTimestamp(audiodescriptionEditor.getLastModified()); } if (oldAudioDescSrc != null && update.getAudioDescription() == null) { update.setAudioDescription(new Mets()); } if (update.getAudioDescription() != null) { try { EditorResult result = audiodescriptionEditor.createResult(); JAXBContext jaxbContext = JAXBContext.newInstance(Mets.class, PremisComplexType.class, NkComplexType.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); marshaller.marshal(update.getAudioDescription(), result); audiodescriptionEditor.write(result, update.getAudioTimestamp(), log); } catch (JAXBException e) { LOG.log(Level.SEVERE, "Unable to unmarshall audiodescription"); } } robj.setLabel(label); robj.flush(); Device device = new Device(); device.setId(id); device.setModel(model); device.setLabel(label); device.setDescription(update.getDescription()); device.setAudioDescription(update.getAudioDescription()); device.setTimestamp(descriptionEditor.getLastModified()); device.setAudioTimestamp(audiodescriptionEditor.getLastModified()); return device; } catch (DigitalObjectConcurrentModificationException ex) { throw new DeviceException(id, ex); } catch (DigitalObjectException ex) { throw new DeviceException(id, ex); } } DeviceRepository(RemoteStorage remoteStorage); Device addDevice(String owner, String model, String label, String log); boolean deleteDevice(String id, String log); List<Device> find(String id); List<Device> find(String id, boolean fetchDescription, int offset); Device update(Device update, String log); List<Device> findAllDevices(int offset); static XmlStreamEditor getMixDescriptionEditor(FedoraObject robj); static XmlStreamEditor getPremisDescriptionEditor(FedoraObject robj); XmlStreamEditor getDescriptionEditor(String id); Mets repairNkComplexType(Mets mets); static final String DESCRIPTION_DS_ID; static final String AUDIODESCRIPTION_DS_ID; static final String DESCRIPTION_DS_LABEL; static final String AUDIO_DESCRIPTION_DS_LABEL; static final String METAMODEL_ID; static final String METAMODEL_AUDIODEVICE_ID; static final String METAMODEL_ID_LABEL; static final String METAMODEL_AUDIODEVICE_ID_LABEL; }
@Test public void testUpdate() throws Exception { Device device = addTestDevice("testUpdate"); String expectedLabel = "updated label"; device.setLabel(expectedLabel); Mix mix = new Mix(); ScannerCapture scanner = new ScannerCapture(); scanner.setScannerManufacturer(MixUtils.stringType("ScannerManufacturer")); ImageCaptureMetadataType imageCaptureMetadata = new ImageCaptureMetadataType(); imageCaptureMetadata.setScannerCapture(scanner); mix.setImageCaptureMetadata(imageCaptureMetadata); device.setDescription(mix); Device update = repository.update(device, "testUpdate"); assertDeviceEquals(device, update); Mix resultDescription = update.getDescription(); assertEquals("ScannerManufacturer", resultDescription.getImageCaptureMetadata().getScannerCapture().getScannerManufacturer().getValue()); List<Device> found = repository.find(update.getId(), true, 0); assertDeviceEquals(update, found); resultDescription = found.get(0).getDescription(); assertEquals(device.getId(), "ScannerManufacturer", resultDescription.getImageCaptureMetadata().getScannerCapture().getScannerManufacturer().getValue()); }