@Override protected void doInitialize(String settingsKey, Map<String, Object> settings) { String clusterName = ObjectUtils.to(String.class, settings.get(CLUSTER_NAME_SUB_SETTING)); Preconditions.checkNotNull(clusterName); String indexName = ObjectUtils.to(String.class, settings.get(INDEX_NAME_SUB_SETTING)); Preconditions.checkNotNull(indexName); String typeName = ObjectUtils.to(String.class, settings.get(TYPE_NAME_SUB_SETTING)); this.indexName = indexName; this.typeName = typeName; this.node = NodeBuilder.nodeBuilder().clusterName(clusterName).client(true).node(); }
private boolean evaluateOne(State state, Object keyValue, List<Object> values) { if (!(keyValue instanceof Recordable || keyValue instanceof UUID)) { Class<?> keyValueClass = keyValue.getClass(); for (ListIterator<Object> i = values.listIterator(); i.hasNext(); ) { i.set(ObjectUtils.to(keyValueClass, i.next())); } } return compare(state, keyValue, values); }
// Reads: key operator value private Predicate readComparison(Queue<String> tokens, ParameterList parameters) { String key = tokens.poll(); if (key == null) { return null; } String realKey = Query.Static.getCanonicalKey(key); if (realKey != null) { key = realKey; } String operator = tokens.poll(); boolean isIgnoreCase = false; if (operator == null) { throw new IllegalArgumentException(String.format("No operator after [%s] key!", key)); } else { operator = operator.toLowerCase(Locale.ENGLISH); if (operator.endsWith("[c]")) { operator = operator.substring(0, operator.length() - 3); isIgnoreCase = true; } } Object value = readValue(tokens); if (value == null) { throw new IllegalArgumentException( String.format("No value after [%s] key and [%s] operator!", key, operator)); } else if (value instanceof String) { String valueString = (String) value; if (valueString.startsWith("?")) { if (valueString.length() == 1) { value = parameters.poll(); } else { String path = valueString.substring(1); int slashAt = path.indexOf('/'); String splitIndex; String splitPath; if (slashAt > -1) { splitIndex = path.substring(0, slashAt); splitPath = path.substring(slashAt + 1); } else { splitIndex = path; splitPath = ""; } Integer index = ObjectUtils.to(Integer.class, splitIndex); if (index == null) { index = 0; } else { path = splitPath; } value = index < parameters.size() ? parameters.get(index) : null; if (value != null && path.length() > 0) { if (value instanceof Recordable) { value = ((Recordable) value).getState().getByPath(path); } else { value = CollectionUtils.getByPath(value, path); } } } } else if ("true".equalsIgnoreCase(valueString)) { value = Boolean.TRUE; } else if ("false".equalsIgnoreCase(valueString)) { value = Boolean.FALSE; } else if ("null".equalsIgnoreCase(valueString)) { value = null; } else if ("missing".equalsIgnoreCase(valueString)) { value = Query.MISSING_VALUE; } } String comparisonOperator = getComparisonOperators().get(operator); if (comparisonOperator != null) { return new ComparisonPredicate( comparisonOperator, isIgnoreCase, key, ObjectUtils.to(Iterable.class, value)); } else { throw new IllegalArgumentException( String.format("[%s] isn't a valid comparison operator!", operator)); } }
public static void writeContents( Database database, BootstrapPackage pkg, Set<ObjectType> additionalTypes, Writer writer, String projectName) throws IOException { boolean first = true; boolean needsObjectTypeMap = false; Set<ObjectType> typeMaps = new HashSet<ObjectType>(); ObjectType objType = database.getEnvironment().getTypeByClass(ObjectType.class); Set<ObjectType> exportTypes = new HashSet<ObjectType>(); Query<?> query = Query.fromAll().using(database); Set<ObjectType> allTypeMappableTypes = new HashSet<ObjectType>(); Set<UUID> concreteTypeIds = new HashSet<UUID>(); for (ObjectType type : database.getEnvironment().getTypes()) { if (!type.as(TypeData.class).getTypeMappableGroups().isEmpty() && type.as(TypeData.class).getTypeMappableUniqueKey() != null) { allTypeMappableTypes.add(type); } } // Package: writer.write(PACKAGE_NAME_HEADER + ": "); writer.write(pkg.getName()); writer.write('\n'); // Project: writer.write(PROJECT_HEADER + ": "); writer.write(projectName); writer.write('\n'); // Date: writer.write(DATE_HEADER + ": "); writer.write(new DateTime().toString("yyyy-MM-dd HH:mm:ss z")); writer.write('\n'); // Types: writer.write(TYPES_HEADER + ": "); if (pkg.isInit()) { writer.write(ALL_TYPES_HEADER_VALUE); writer.write('\n'); } else { exportTypes.addAll(getAllTypes(database, pkg)); exportTypes.addAll(getAllTypes(database, additionalTypes)); for (ObjectType typeMappableType : allTypeMappableTypes) { GETTYPEMAPPABLETYPE: for (ObjectType type : exportTypes) { for (String group : typeMappableType.as(TypeData.class).getTypeMappableGroups()) { if (type.getGroups().contains(group)) { typeMaps.add(typeMappableType); break GETTYPEMAPPABLETYPE; } } } } GETOBJECTTYPE: for (ObjectType type : exportTypes) { for (ObjectField field : type.getFields()) { if (field.getTypes().contains(objType)) { needsObjectTypeMap = true; break GETOBJECTTYPE; } } } for (ObjectType exportType : exportTypes) { String clsName = exportType.getObjectClassName(); if (!ObjectUtils.isBlank(clsName)) { for (ObjectType concreteType : database.getEnvironment().getTypesByGroup(clsName)) { if (!concreteType.isAbstract()) { concreteTypeIds.add(concreteType.getId()); } } } } if (exportTypes.contains(objType)) { if (!first) { writer.write(','); } else { first = false; } writer.write(objType.getInternalName()); } for (ObjectType type : exportTypes) { if (type.equals(objType)) { continue; } if (!first) { writer.write(','); } else { first = false; } writer.write(type.getInternalName()); } writer.write('\n'); } // Determine if there are any fields that need references followed Map<UUID, Map<String, ObjectType>> followReferences = new HashMap<UUID, Map<String, ObjectType>>(); if (!pkg.isInit()) { for (ObjectType type : exportTypes) { for (String fieldName : type.as(TypeData.class).getFollowReferencesFields()) { ObjectField field = type.getField(fieldName); if (field != null) { for (ObjectType fieldType : field.getTypes()) { if (!exportTypes.contains(fieldType)) { if (!followReferences.containsKey(type.getId())) { followReferences.put(type.getId(), new HashMap<String, ObjectType>()); } followReferences.get(type.getId()).put(fieldName, fieldType); } } } } } } if (!typeMaps.isEmpty()) { for (ObjectType typeMapType : typeMaps) { String clsName = typeMapType.getObjectClassName(); if (!ObjectUtils.isBlank(clsName)) { for (ObjectType type : database.getEnvironment().getTypesByGroup(clsName)) { concreteTypeIds.remove(type.getId()); } } } } // Mapping Types: if (pkg.isInit() || needsObjectTypeMap || !typeMaps.isEmpty()) { first = true; writer.write(TYPE_MAP_HEADER + ": "); if (pkg.isInit() || needsObjectTypeMap) { writer.write(objType.getInternalName()); writer.write("/internalName"); first = false; } for (ObjectType typeMapType : typeMaps) { if (!first) { writer.write(','); } else { first = false; } writer.write(typeMapType.getInternalName()); writer.write('/'); writer.write(typeMapType.as(TypeData.class).getTypeMappableUniqueKey()); } writer.write('\n'); } // Row Count: Long count = 0L; try { if (concreteTypeIds.isEmpty()) { count = Query.fromAll().using(database).noCache().count(); } else { for (UUID concreteTypeId : concreteTypeIds) { long concreteCount = Query.fromAll() .using(database) .noCache() .where("_type = ?", concreteTypeId) .count(); count = count + concreteCount; } if (needsObjectTypeMap) { long objectCount = Query.fromAll().using(database).noCache().where("_type = ?", objType).count(); count = count + objectCount; } if (!typeMaps.isEmpty()) { for (ObjectType typeMapType : typeMaps) { long typeMapCount = Query.fromAll().using(database).noCache().where("_type = ?", typeMapType).count(); count = count + typeMapCount; } } } writer.write(ROW_COUNT_HEADER + ": "); writer.write(ObjectUtils.to(String.class, count)); writer.write('\n'); } catch (RuntimeException e) { // Count query timed out. Just don't write the row count header. count = null; } // blank line between headers and data writer.write('\n'); writer.flush(); // ObjectType records first if (exportTypes.isEmpty() || exportTypes.contains(objType) || needsObjectTypeMap) { for (Object r : Query.fromType(objType) .using(database) .noCache() .resolveToReferenceOnly() .iterable(100)) { writer.write(ObjectUtils.toJson(((Recordable) r).getState().getSimpleValues(true))); writer.write('\n'); } } // Then other mapping types for (ObjectType typeMapType : typeMaps) { for (Object r : Query.fromType(typeMapType) .using(database) .noCache() .resolveToReferenceOnly() .iterable(100)) { writer.write(ObjectUtils.toJson(((Recordable) r).getState().getSimpleValues(true))); writer.write('\n'); } } // Then everything else if (pkg.isInit()) { concreteTypeIds.clear(); // should already be empty concreteTypeIds.add(null); } UUID lastTypeId = null; Set<UUID> seenIds = new HashSet<UUID>(); query.getOptions().put(SqlDatabase.USE_JDBC_FETCH_SIZE_QUERY_OPTION, false); for (UUID typeId : concreteTypeIds) { Query<?> concreteQuery = query.clone(); if (typeId != null) { concreteQuery.where("_type = ?", typeId); } for (Object o : concreteQuery.noCache().resolveToReferenceOnly().iterable(100)) { if (o instanceof Recordable) { Recordable r = (Recordable) o; writer.write(ObjectUtils.toJson(r.getState().getSimpleValues(true))); writer.write('\n'); if (!pkg.isInit()) { if (lastTypeId == null || !lastTypeId.equals(r.getState().getTypeId())) { seenIds.clear(); } else if (seenIds.size() > MAX_SEEN_REFERENCE_IDS_SIZE) { seenIds.clear(); } lastTypeId = r.getState().getTypeId(); Map<String, ObjectType> followReferencesFieldMap; if ((followReferencesFieldMap = followReferences.get(r.getState().getTypeId())) != null) { for (Map.Entry<String, ObjectType> entry : followReferencesFieldMap.entrySet()) { Object reference = r.getState().getRawValue(entry.getKey()); Set<UUID> referenceIds = new HashSet<UUID>(); if (reference instanceof Collection) { for (Object referenceObj : ((Collection<?>) reference)) { if (referenceObj instanceof Recordable) { UUID referenceUUID = ObjectUtils.to( UUID.class, ((Recordable) referenceObj).getState().getId()); if (referenceUUID != null) { if (!seenIds.contains(referenceUUID)) { referenceIds.add(referenceUUID); } } } } } else if (reference instanceof Recordable) { UUID referenceUUID = ObjectUtils.to(UUID.class, ((Recordable) reference).getState().getId()); if (referenceUUID != null) { if (!seenIds.contains(referenceUUID)) { referenceIds.add(referenceUUID); } } } if (!referenceIds.isEmpty()) { for (Object ref : Query.fromType(entry.getValue()) .noCache() .using(database) .where("_id = ?", referenceIds) .selectAll()) { if (ref instanceof Recordable) { Recordable refr = (Recordable) ref; seenIds.add(refr.getState().getId()); writer.write(ObjectUtils.toJson(refr.getState().getSimpleValues(true))); writer.write('\n'); } } } } } } } } } writer.flush(); }
@Override protected void doService(ToolPageContext page) throws IOException, ServletException { ToolUser user = page.getUser(); Collection<String> includeFields = Arrays.asList("returnToDashboardOnSave"); Object object = Query.from(Object.class).where("_id = ?", page.param(UUID.class, "id")).first(); State state = State.getInstance(object); ContentLock contentLock = null; if (object != null) { contentLock = ContentLock.Static.lock(object, null, user); } if (page.isFormPost()) { if (page.param(String.class, "action-edits") != null) { if (state != null) { Date newPublishDate = page.param(Date.class, "publishDate"); if (newPublishDate != null) { Content.ObjectModification contentData = state.as(Content.ObjectModification.class); DateTimeZone timeZone = page.getUserDateTimeZone(); newPublishDate = new Date( DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss") .withZone(timeZone) .parseMillis(new DateTime(newPublishDate).toString("yyyy-MM-dd HH:mm:ss"))); contentData.setPublishUser(page.getUser()); contentData.setPublishDate(newPublishDate); state.save(); } } } else if (page.param(String.class, "action-unlock") != null) { contentLock.delete(); page.writeStart("script", "type", "text/javascript"); page.writeRaw("window.location.reload();"); page.writeEnd(); } else if (page.param(String.class, "action-settings") != null) { try { page.include("/WEB-INF/objectPost.jsp", "object", user, "includeFields", includeFields); user.save(); } catch (Exception error) { page.getErrors().add(error); } } } String returnUrl = page.param(String.class, "returnUrl"); page.writeHeader(); page.writeStart("style", "type", "text/css"); page.writeCss(".cms-contentTools th", "width", "25%;"); page.writeEnd(); page.writeStart("div", "class", "widget cms-contentTools"); page.writeStart("h1", "class", "icon icon-wrench"); page.writeHtml("Tools"); page.writeEnd(); page.writeStart("div", "class", "tabbed"); page.writeStart("div", "class", "fixedScrollable", "data-tab", "For Editors"); if (object != null) { Content.ObjectModification contentData = state.as(Content.ObjectModification.class); Date publishDate = contentData.getPublishDate(); ToolUser publishUser = contentData.getPublishUser(); Date updateDate = contentData.getUpdateDate(); ToolUser updateUser = contentData.getUpdateUser(); page.writeStart("table", "class", "table-striped"); page.writeStart("tbody"); if (publishDate != null || publishUser != null) { page.writeStart("tr"); page.writeStart("th"); page.writeHtml("Published"); page.writeEnd(); page.writeStart("td"); if (publishDate != null) { page.writeHtml(page.formatUserDateTime(publishDate)); } page.writeEnd(); page.writeStart("td"); if (publishUser != null) { page.writeObjectLabel(publishUser); } page.writeEnd(); page.writeEnd(); } if (updateDate != null || updateUser != null) { page.writeStart("tr"); page.writeStart("th"); page.writeHtml("Last Updated"); page.writeEnd(); page.writeStart("td"); if (updateDate != null) { page.writeHtml(page.formatUserDateTime(updateDate)); } page.writeEnd(); page.writeStart("td"); if (updateUser != null) { page.writeObjectLabel(updateUser); } page.writeEnd(); page.writeEnd(); } page.writeEnd(); page.writeEnd(); page.writeStart("h2"); page.writeHtml("Advanced Edits"); page.writeEnd(); if (page.isFormPost() && page.param(String.class, "action-edits") != null) { if (page.getErrors().isEmpty()) { page.writeStart("div", "class", "message message-success"); page.writeHtml("Advanced edits successfully saved."); page.writeEnd(); } else { page.include("/WEB-INF/errors.jsp"); } } page.writeStart("form", "method", "post", "action", page.url("")); page.writeStart("div", "class", "inputContainer"); page.writeStart("div", "class", "inputLabel"); page.writeStart("label", "for", page.createId()); page.writeHtml("New Publish Date"); page.writeEnd(); page.writeEnd(); page.writeStart("div", "class", "inputSmall"); page.writeElement( "input", "type", "text", "class", "date", "name", "publishDate", "value", page.formatUserDateTime(publishDate)); page.writeEnd(); page.writeEnd(); page.writeStart("div", "class", "actions"); page.writeStart( "button", "class", "icon icon-action-save", "name", "action-edits", "value", true); page.writeHtml("Save"); page.writeEnd(); page.writeEnd(); page.writeEnd(); if (!user.equals(contentLock.getOwner())) { page.writeStart("h2"); page.writeHtml("Content Lock"); page.writeEnd(); page.writeStart("div", "class", "message message-warning"); page.writeStart("p"); page.writeHtml("Locked by "); page.writeObjectLabel(contentLock.getOwner()); page.writeHtml(" since "); page.writeHtml(page.formatUserDateTime(contentLock.getCreateDate())); page.writeHtml("."); page.writeEnd(); page.writeEnd(); page.writeStart("form", "method", "post", "action", page.url("")); page.writeStart("div", "class", "actions"); page.writeStart( "button", "class", "icon icon-unlock", "name", "action-unlock", "value", true); page.writeHtml("Unlock"); page.writeEnd(); page.writeEnd(); page.writeEnd(); } } page.writeStart("h2"); page.writeHtml("Settings"); page.writeEnd(); if (page.isFormPost() && page.param(String.class, "action-settings") != null) { if (page.getErrors().isEmpty()) { page.writeStart("div", "class", "message message-success"); page.writeHtml("Settings successfully saved."); page.writeEnd(); } else { page.include("/WEB-INF/errors.jsp"); } } page.writeStart("form", "method", "post", "action", page.url(""), "style", "margin-bottom:0;"); page.include("/WEB-INF/objectForm.jsp", "object", user, "includeFields", includeFields); page.writeStart("div", "class", "actions"); page.writeStart( "button", "class", "icon icon-action-save", "name", "action-settings", "value", true); page.writeHtml("Save"); page.writeEnd(); page.writeEnd(); page.writeEnd(); page.writeEnd(); page.writeStart("div", "class", "fixedScrollable", "data-tab", "For Developers"); page.writeStart("ul"); if (object != null) { page.writeStart("li"); page.writeStart("a", "target", "_blank", "href", page.objectUrl("/contentRaw", object)); page.writeHtml("View Raw Data"); page.writeEnd(); page.writeEnd(); } if (!ObjectUtils.isBlank(returnUrl)) { page.writeStart("li"); if (ObjectUtils.to( boolean.class, StringUtils.getQueryParameterValue(returnUrl, "deprecated"))) { page.writeStart( "a", "target", "_top", "href", StringUtils.addQueryParameters(returnUrl, "deprecated", null)); page.writeHtml("Hide Deprecated Fields"); page.writeEnd(); } else { page.writeStart( "a", "target", "_top", "href", StringUtils.addQueryParameters(returnUrl, "deprecated", true)); page.writeHtml("Show Deprecated Fields"); page.writeEnd(); } page.writeEnd(); } page.writeEnd(); if (object != null) { ObjectType type = state.getType(); page.writeStart("table", "class", "table-striped"); page.writeStart("tbody"); if (type != null) { Class<?> objectClass = type.getObjectClass(); if (objectClass != null) { page.writeStart("tr"); page.writeStart("th"); page.writeStart("label", "for", page.createId()); page.writeHtml("Class"); page.writeEnd(); page.writeEnd(); page.writeStart("td"); page.writeJavaClassLink(objectClass); page.writeEnd(); page.writeEnd(); } } page.writeStart("tr"); page.writeStart("th"); page.writeStart("label", "for", page.createId()); page.writeHtml("ID"); page.writeEnd(); page.writeEnd(); page.writeStart("td"); page.writeElement( "input", "type", "text", "id", page.getId(), "class", "code", "value", state.getId(), "readonly", "readonly", "style", "width:100%;", "onclick", "this.select();"); page.writeEnd(); page.writeEnd(); page.writeStart("tr"); page.writeStart("th"); page.writeStart("label", "for", page.createId()); page.writeHtml("URL"); page.writeEnd(); page.writeEnd(); page.writeStart("td"); page.writeElement( "input", "type", "text", "id", page.getId(), "value", JspUtils.getAbsoluteUrl( page.getRequest(), page.cmsUrl("/content/edit.jsp", "id", state.getId())), "readonly", "readonly", "style", "width:100%;", "onclick", "this.select();"); page.writeEnd(); page.writeEnd(); page.writeEnd(); page.writeEnd(); } if (object != null) { ObjectType type = state.getType(); if (type != null) { if (!ObjectUtils.isBlank(type.as(Renderer.TypeModification.class).getEmbedPath())) { String permalink = state.as(Directory.ObjectModification.class).getPermalink(); if (!ObjectUtils.isBlank(permalink)) { String siteUrl = Application.Static.getInstance(CmsTool.class).getDefaultSiteUrl(); StringBuilder embedCode = new StringBuilder(); embedCode.append("<script type=\"text/javascript\" src=\""); embedCode.append( StringUtils.addQueryParameters( StringUtils.removeEnd(siteUrl, "/") + permalink, "_embed", true, "_format", "js")); embedCode.append("\"></script>"); page.writeHtml("Embed Code:"); page.writeElement("br"); page.writeStart( "textarea", "class", "code", "data-expandable-class", "code", "readonly", "readonly", "onclick", "this.select();"); page.writeHtml(embedCode); page.writeEnd(); } } String defaultPath = type.as(Renderer.TypeModification.class).getPath(); Map<String, String> paths = type.as(Renderer.TypeModification.class).getPaths(); if (!ObjectUtils.isBlank(defaultPath) || !ObjectUtils.isBlank(paths)) { page.writeStart("h2"); page.writeHtml("Renderers"); page.writeEnd(); page.writeStart("table", "class", "table-striped"); page.writeStart("tbody"); if (!ObjectUtils.isBlank(defaultPath)) { page.writeStart("tr"); page.writeStart("th"); page.writeStart("code"); page.writeHtml("Default"); page.writeEnd(); page.writeEnd(); page.writeStart("td"); page.writeStart("code"); page.writeStart( "a", "target", "_blank", "href", DebugFilter.Static.getServletPath( page.getRequest(), "code", "action", "edit", "type", "JSP", "servletPath", defaultPath)); page.writeHtml(defaultPath); page.writeEnd(); page.writeEnd(); page.writeEnd(); page.writeEnd(); } for (Map.Entry<String, String> entry : paths.entrySet()) { page.writeStart("tr"); page.writeStart("th"); page.writeStart("code"); page.writeHtml(entry.getKey()); page.writeEnd(); page.writeEnd(); page.writeStart("td"); page.writeStart("code"); page.writeStart( "a", "target", "_blank", "href", DebugFilter.Static.getServletPath( page.getRequest(), "code", "action", "edit", "type", "JSP", "servletPath", entry.getValue())); page.writeHtml(entry.getValue()); page.writeEnd(); page.writeEnd(); page.writeEnd(); page.writeEnd(); } page.writeEnd(); page.writeEnd(); } Class<?> objectClass = type.getObjectClass(); if (objectClass != null) { Static.writeJavaAnnotationDescriptions(page, objectClass); } } } page.writeEnd(); page.writeEnd(); page.writeEnd(); page.writeFooter(); }