class RestructureWebrootIndex extends AbstractHighLevelChange {

	@Override
	public void apply() {
		NodeDaoWrapper nodeDao = Tx.get().data().nodeDao();
		ContentDaoWrapper contentDao = Tx.get().data().contentDao();

		log.info("Applying change: " + getName());
		FramedTransactionalGraph graph = Tx.getActive().getGraph();
		Iterable<? extends GraphFieldContainerEdgeImpl> edges = graph.getFramedEdgesExplicit("@class", HAS_FIELD_CONTAINER,
			GraphFieldContainerEdgeImpl.class);
		long count = 0;
		long total = 0;
		for (GraphFieldContainerEdgeImpl edge : edges) {
			ContainerType type = edge.getType();
			if (DRAFT.equals(type) || PUBLISHED.equals(type)) {
				String branchUuid = edge.getBranchUuid();
				NodeGraphFieldContainer container = edge.getNodeContainer();
				// Skip graph inconsistencies
				if (container == null) {
					continue;
				}
				edge.setUrlFieldInfo(contentDao.getUrlFieldValues(container).collect(Collectors.toSet()));
				String segment = contentDao.getSegmentFieldValue(container);
				if (segment != null && !segment.trim().isEmpty()) {
					HibNode node = contentDao.getNode(container);
					if (node != null) {
						node = nodeDao.getParentNode(node, branchUuid);
					}
					String newInfo = GraphFieldContainerEdgeImpl.composeSegmentInfo(node, segment);
					edge.setSegmentInfo(newInfo);
				} else {
					edge.setSegmentInfo(null);
				}
				if (count % 100 == 0) {
					log.info("Updated {" + count + "} content edges. Processed {" + total + "} edges in total");
				}
				count++;
			}

			String segment = edge.getSegmentInfo();
			if (segment == null || segment.trim().isEmpty()) {
				edge.setSegmentInfo(null);
			}
			if (total % 1000 == 0) {
				graph.commit();
			}
			total++;
		}
		log.info("Done updating all content edges. Updated: {" + count + "} of {" + total + "}");

		Iterable<? extends NodeGraphFieldContainerImpl> containers = graph.getFramedVertices("@class",
			NodeGraphFieldContainerImpl.class.getSimpleName(), NodeGraphFieldContainerImpl.class);
		for (NodeGraphFieldContainer container : containers) {
			container.getElement().removeProperty("publishedWebrootUrlInfo");
			container.getElement().removeProperty("webrootUrlInfo");
			container.getElement().removeProperty("publishedWebrootPathInfo");
			container.getElement().removeProperty("webrootPathInfo");
		}

	}

	@Inject public  RestructureWebrootIndex(Database db);

}