@Override public <S extends StreamDefinition> S save(S entity) { try { Map<String, String> map = new HashMap<>(); map.put(DEFINITION_KEY, entity.getDefinition()); map.put( MODULE_DEFINITIONS_KEY, objectWriter.writeValueAsString(entity.getModuleDefinitions())); CuratorFramework client = zkConnection.getClient(); String path = Paths.build(Paths.STREAMS, entity.getName()); byte[] binary = ZooKeeperUtils.mapToBytes(map); BackgroundPathAndBytesable<?> op = client.checkExists().forPath(path) == null ? client.create() : client.setData(); op.forPath(path, binary); logger.trace("Saved stream {} with properties {}", path, map); StreamDefinitionRepositoryUtils.saveDependencies(moduleDependencyRepository, entity); } catch (Exception e) { // NodeExistsException indicates that we tried to create the // path just after another thread/jvm successfully created it ZooKeeperUtils.wrapAndThrowIgnoring(e, NodeExistsException.class); } return entity; }
@Override public void loadAllEntries() { final Connection connection = getConnectionpool().getConnection(); if (connection == null) return; Statement query = null; try { query = connection.createStatement(); final ResultSet result = query.executeQuery("SELECT * FROM `" + tableName + "` WHERE 1=1"); while (result.next()) try { final S data = constructor.newInstance(result, columnNames); datas.put(data.getName().toLowerCase(), data); } catch (final InvocationTargetException e) { shortPrintStackTrace(e, e.getCause()); } catch (final Exception e) { e.printStackTrace(); } result.close(); } catch (final SQLException e) { } finally { if (query != null) try { query.close(); } catch (final Exception e) { } getConnectionpool().releaseConnection(connection); } }
@Override public void save(final S entry) { if (entry == null) return; super.save(entry); entries.put( entry.getName(), ChatHelper.listingString(DATASEPARATOR, entry.saveToFlatDatabase()) + LINESEPERATOR); asyncSaveDatabase(); }
@Override public void saveAll(final Collection<S> entries) { for (final S entry : entries) { super.save(entry); this.entries.put( entry.getName(), ChatHelper.listingString(DATASEPARATOR, entry.saveToFlatDatabase()) + LINESEPERATOR); } asyncSaveDatabase(); }
@Override public S loadEntry(final String key) { final Connection connection = getConnectionpool().getConnection(); if (connection == null) return null; Statement query = null; try { query = connection.createStatement(); final ResultSet result = query.executeQuery( "SELECT * FROM `" + tableName + "` WHERE " + columnNames[0] + "='" + key + "' LIMIT 1"); if (result.next()) try { final S data = constructor.newInstance(result, columnNames); datas.put(data.getName().toLowerCase(), data); result.close(); return data; } catch (final InvocationTargetException e) { System.err.println("Error occured while trying to load entry: " + key); shortPrintStackTrace(e, e.getCause()); } catch (final Exception e) { System.err.println("Error occured while trying to load entry: " + key); e.printStackTrace(); } result.close(); return null; } catch (final SQLException e) { return null; } finally { if (query != null) try { query.close(); } catch (final Exception e) { } getConnectionpool().releaseConnection(connection); } }
@Override public void save(final S entry) { entry.saveToConfigDatabase(config, table + "." + entry.getName() + ".", columnNames); }
@Override public Object set( final SecurityContext securityContext, final NodeInterface targetNode, final Iterable<S> collection) throws FrameworkException { final App app = StructrApp.getInstance(securityContext); final PropertyMap properties = new PropertyMap(); final NodeInterface actualTargetNode = (NodeInterface) unwrap(securityContext, relation.getClass(), targetNode, properties); final Set<S> toBeDeleted = new LinkedHashSet<>(Iterables.toList(get(securityContext, actualTargetNode, null))); final Set<S> toBeCreated = new LinkedHashSet<>(); if (collection != null) { Iterables.addAll(toBeCreated, collection); } // create intersection of both sets final Set<S> intersection = new HashSet<>(toBeCreated); intersection.retainAll(toBeDeleted); // intersection needs no change toBeCreated.removeAll(intersection); toBeDeleted.removeAll(intersection); // remove existing relationships for (S sourceNode : toBeDeleted) { for (AbstractRelationship rel : actualTargetNode.getIncomingRelationships()) { final String relTypeName = rel.getRelType().name(); final String desiredRelType = relation.name(); if (sourceNode.equals(actualTargetNode)) { logger.warn( "Preventing deletion of self relationship {}-[{}]->{}. If you experience issue with this, please report to [email protected].", new Object[] {sourceNode, rel.getRelType(), actualTargetNode}); // skip self relationships continue; } if (relTypeName.equals(desiredRelType) && rel.getSourceNode().equals(sourceNode)) { app.delete(rel); } } } final List<Relation> createdRelationship = new LinkedList<>(); // create new relationships for (S sourceNode : toBeCreated) { if (sourceNode != null && actualTargetNode != null) { properties.clear(); final S actualSourceNode = (S) unwrap(securityContext, relation.getClass(), sourceNode, properties); final PropertyMap notionProperties = getNotionProperties( securityContext, relation.getClass(), actualSourceNode.getName() + relation.name() + actualTargetNode.getName()); if (notionProperties != null) { properties.putAll(notionProperties); } relation.ensureCardinality(securityContext, actualSourceNode, actualTargetNode); createdRelationship.add( app.create(actualSourceNode, actualTargetNode, relation.getClass(), properties)); } } return createdRelationship; }