BrowseNodeEntityAdapter extends IterableEntityAdapter<OrientBrowseNode> { public void createAssetNode(final ODatabaseDocumentTx db, final String repositoryName, final String format, final List<? extends BrowsePath> paths, final Asset asset) { maybeCreateParentNodes(db, repositoryName, format, paths.subList(0, paths.size() - 1)); OrientBrowseNode node = newNode(repositoryName, format, paths); ODocument document = findNodeRecord(db, node); if (document == null) { node.setAssetId(EntityHelper.id(asset)); addEntity(db, node); } else { ORID oldAssetId = document.field(P_ASSET_ID, ORID.class); ORID newAssetId = assetEntityAdapter.recordIdentity(asset); if (oldAssetId == null) { document.field(P_ASSET_ID, newAssetId); String path = document.field(P_PATH, OType.STRING); if (!asset.name().endsWith("/") && path.endsWith("/")) { path = path.substring(0, path.length() - 1); document.field(P_PATH, path); } document.save(); } else if (!oldAssetId.equals(newAssetId)) { throw new BrowseNodeCollisionException("Node already has an asset"); } } } @Inject  BrowseNodeEntityAdapter(final ComponentEntityAdapter componentEntityAdapter,
                                 final AssetEntityAdapter assetEntityAdapter,
                                 final PackageUrlService packageUrlService); void createComponentNode(final ODatabaseDocumentTx db,
                                  final String repositoryName,
                                  final String format,
                                  final List<? extends BrowsePath> paths,
                                  final Component component); void createAssetNode(final ODatabaseDocumentTx db,
                              final String repositoryName,
                              final String format,
                              final List<? extends BrowsePath> paths,
                              final Asset asset); boolean assetNodeExists(final ODatabaseDocumentTx db, final EntityId assetId); void deleteComponentNode(final ODatabaseDocumentTx db, final EntityId componentId); void deleteAssetNode(final ODatabaseDocumentTx db, final EntityId assetId); int deleteByRepository(final ODatabaseDocumentTx db, final String repositoryName, final int limit); List<OrientBrowseNode> getByPath(
      final ODatabaseDocumentTx db,
      final String repositoryName,
      final List<String> path,
      final int maxNodes,
      final String assetFilter,
      final Map<String, Object> filterParameters); @Override boolean resolveConflicts(); static final String P_REPOSITORY_NAME; static final String P_FORMAT; static final String P_PATH; static final String P_PARENT_PATH; static final String P_NAME; static final String P_PACKAGE_URL; static final String P_COMPONENT_ID; static final String P_ASSET_ID; static final String AUTHZ_REPOSITORY_NAME; }
@Test public void duplicateRequestsAreMerged() throws Exception { List<String> path = Splitter.on('/').omitEmptyStrings().splitToList(asset.name()); try (ODatabaseDocumentTx db = database.getInstance().acquire()) { underTest.createAssetNode(db, REPOSITORY_NAME, FORMAT_NAME, toBrowsePaths(path), asset); underTest.createAssetNode(db, REPOSITORY_NAME, FORMAT_NAME, toBrowsePaths(path), asset); underTest.createAssetNode(db, REPOSITORY_NAME, FORMAT_NAME, toBrowsePaths(path), asset); assertThat(underTest.browse(db), containsInAnyOrder( allOf( hasProperty("repositoryName", is(REPOSITORY_NAME)), hasProperty("parentPath", is("/")), hasProperty("name", is("org")), hasProperty("componentId", nullValue()), hasProperty("assetId", nullValue()), hasProperty("path", is("org/"))) , allOf( hasProperty("repositoryName", is(REPOSITORY_NAME)), hasProperty("parentPath", is("/org/")), hasProperty("name", is("foo")), hasProperty("componentId", nullValue()), hasProperty("assetId", nullValue()), hasProperty("path", is("org/foo/"))) , allOf( hasProperty("repositoryName", is(REPOSITORY_NAME)), hasProperty("parentPath", is("/org/foo/")), hasProperty("name", is("1.0")), hasProperty("componentId", nullValue()), hasProperty("assetId", nullValue()), hasProperty("path", is("org/foo/1.0/"))) , allOf( hasProperty("repositoryName", is(REPOSITORY_NAME)), hasProperty("parentPath", is("/org/foo/1.0/")), hasProperty("name", is("foo-1.0.jar")), hasProperty("componentId", nullValue()), hasProperty("assetId", is(EntityHelper.id(asset))), hasProperty("path", is("org/foo/1.0/foo-1.0.jar"))) )); } }