Create extends NotInReadOnlyModeService { public Element serviceSpecificExec(Element params, ServiceContext context) throws Exception { GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME); DataManager dm = gc.getBean(DataManager.class); String child = Util.getParam(params, Params.CHILD, "n"); String isTemplate = Util.getParam(params, Params.TEMPLATE, "n"); String id; String uuid; boolean haveAllRights = Boolean.valueOf(Util.getParam(params, Params.FULL_PRIVILEGES, "false")); SettingManager sm = gc.getBean(SettingManager.class); boolean generateUuid = sm.getValueAsBool(Settings.SYSTEM_METADATACREATE_GENERATE_UUID); try { uuid = Util.getParam(params, Params.UUID); id = dm.getMetadataId(uuid); } catch (BadInputEx x) { try { id = Util.getParam(params, Params.ID); uuid = dm.getMetadataUuid(id); } catch (BadInputEx xx) { throw new Exception("Request must contain a UUID or an ID"); } } String metadataUuid; if (!generateUuid) { metadataUuid = Util.getParam(params, "metadataUuid", ""); if (StringUtils.isEmpty(metadataUuid)) { metadataUuid = UUID.randomUUID().toString(); } else { if (StringUtils.isNotEmpty(dm.getMetadataId(metadataUuid))) { throw new Exception("The metadata UUID already exists. Choose another one"); } } } else { metadataUuid = UUID.randomUUID().toString(); } String groupOwner = Util.getParam(params, Params.GROUP); UserSession user = context.getUserSession(); if (user.getProfile() != Profile.Administrator) { final Specifications<UserGroup> spec = where(UserGroupSpecs.hasProfile(Profile.Editor)) .and(UserGroupSpecs.hasUserId(user.getUserIdAsInt())) .and(UserGroupSpecs.hasGroupId(Integer.valueOf(groupOwner))); final List<UserGroup> userGroups = context.getBean(UserGroupRepository.class).findAll(spec); if (userGroups.size() == 0) { throw new ServiceNotAllowedEx("Service not allowed. User needs to be Editor in group with id " + groupOwner); } } SettingManager settingManager = gc.getBean(SettingManager.class); String newId = dm.createMetadata(context, id, groupOwner, settingManager.getSiteId(), context.getUserSession().getUserIdAsInt(), (child.equals("n") ? null : uuid), isTemplate, haveAllRights, metadataUuid); dm.activateWorkflowIfConfigured(context, newId, groupOwner); try { StoreUtils.copyDataDir(context, Integer.parseInt(id), Integer.parseInt(newId), true); } catch (Exception e) { Log.warning(Geonet.DATA_MANAGER, "Error while copying metadata resources. " + e.toString() + ". Metadata is created but without resources from record with id:" + id); } Element response = new Element(Jeeves.Elem.RESPONSE); response.addContent(new Element(Geonet.Elem.JUSTCREATED).setText("true")); String sessionTabProperty = useEditTab ? Geonet.Session.METADATA_EDITING_TAB : Geonet.Session.METADATA_SHOW; Element elCurrTab = params.getChild(Params.CURRTAB); if (elCurrTab != null) { context.getUserSession().setProperty(sessionTabProperty, elCurrTab.getText()); } response.addContent(new Element(Geonet.Elem.ID).setText(newId)); return response; }  void init(Path appPath, ServiceConfig params); Element serviceSpecificExec(Element params, ServiceContext context);  }
@Test public void testCreateNormalMetadata() throws Exception { final ServiceContext context = createServiceContext(); loginAsAdmin(context); final String id = importMetadata(context); int sampleGroup = _groupRepo.findByName("sample").getId(); final Element params = createParams( read(Geonet.Elem.ID, id), read(Geonet.Elem.GROUP, sampleGroup)); final Create createService = new Create(); final Element element = createService.serviceSpecificExec(params, context); assertEquals(2, _metadataRepo.count()); assertNotNull(_metadataRepo.findOne(element.getChildText(Geonet.Elem.ID))); }
@Test public void testCreateMetadataAndCopyExistingPictures() throws Exception { final ServiceContext context = createServiceContext(); loginAsAdmin(context); final String id = importMetadata(context); Path mdPublicDataDir = Lib.resource.getDir(context, Params.Access.PUBLIC, Integer.parseInt(id)); Path mdPrivateDataDir = Lib.resource.getDir(context, Params.Access.PRIVATE, Integer.parseInt(id)); final Path smallImage = mdPublicDataDir.resolve("small.gif"); final Path largeImage = mdPublicDataDir.resolve("large.gif"); createImage(GIF, smallImage); createImage(GIF, largeImage); final Path privateImage = mdPrivateDataDir.resolve("privateFile.gif"); createImage(GIF, privateImage); _dataManager.setThumbnail(context, id, true, smallImage.toAbsolutePath().normalize().toString(), false); _dataManager.setThumbnail(context, id, false, largeImage.toAbsolutePath().normalize().toString(), false); int sampleGroup = _groupRepo.findByName("sample").getId(); final Element params = createParams( read(Geonet.Elem.ID, id), read(Geonet.Elem.GROUP, sampleGroup)); final Create createService = new Create(); final Element element = createService.serviceSpecificExec(params, context); assertEquals(2, _metadataRepo.count()); final String newId = element.getChildText(Geonet.Elem.ID); assertNotNull(_metadataRepo.findOne(newId)); Path newPublicMdDataDir = Lib.resource.getDir(context, Params.Access.PUBLIC, Integer.parseInt(newId)); assertTrue(Files.exists(newPublicMdDataDir.resolve(smallImage.getFileName()))); assertTrue(Files.exists(newPublicMdDataDir.resolve(largeImage.getFileName()))); final int expected = 2; assertFilesInDirectory(newPublicMdDataDir, expected); Path newPrivateMdDataDir = Lib.resource.getDir(context, Params.Access.PRIVATE, Integer.parseInt(newId)); assertTrue(Files.exists(newPrivateMdDataDir)); assertFilesInDirectory(newPrivateMdDataDir, 1); assertTrue(Files.exists(newPrivateMdDataDir.resolve(privateImage.getFileName()))); }
@Test public void testCreateMetadataAndPicturesAreMissing() throws Exception { final ServiceContext context = createServiceContext(); loginAsAdmin(context); final String id = importMetadata(context); _dataManager.setThumbnail(context, id, true, "abc/small.gif", false); _dataManager.setThumbnail(context, id, false, "abc/large.gif", false); int sampleGroup = _groupRepo.findByName("sample").getId(); final Element params = createParams( read(Geonet.Elem.ID, id), read(Geonet.Elem.GROUP, sampleGroup)); final Create createService = new Create(); final Element element = createService.serviceSpecificExec(params, context); assertEquals(2, _metadataRepo.count()); assertNotNull(_metadataRepo.findOne(element.getChildText(Geonet.Elem.ID))); }