@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((cachedQuery == null) ? 0 : cachedQuery.hashCode()); result = prime * result + ((dbPath == null) ? 0 : dbPath.hashCode()); result = prime * result + (descending ? 1231 : 1237); result = prime * result + ((designDocId == null) ? 0 : designDocId.hashCode()); result = prime * result + ((endDocId == null) ? 0 : endDocId.hashCode()); result = prime * result + ((endKey == null) ? 0 : endKey.hashCode()); result = prime * result + (group ? 1231 : 1237); result = prime * result + groupLevel; result = prime * result + (ignoreNotFound ? 1231 : 1237); result = prime * result + (includeDocs ? 1231 : 1237); result = prime * result + (inclusiveEnd ? 1231 : 1237); result = prime * result + ((key == null) ? 0 : key.hashCode()); result = prime * result + limit; result = prime * result + ((listName == null) ? 0 : listName.hashCode()); result = prime * result + ((queryParams == null) ? 0 : queryParams.hashCode()); result = prime * result + (reduce ? 1231 : 1237); result = prime * result + skip; result = prime * result + ((staleOk == null) ? 0 : staleOk.hashCode()); result = prime * result + ((startDocId == null) ? 0 : startDocId.hashCode()); result = prime * result + ((startKey == null) ? 0 : startKey.hashCode()); result = prime * result + ((viewName == null) ? 0 : viewName.hashCode()); return result; }
/** * Generates views based on annotations found in a repository class. If the repository class * extends org.ektorp.support.CouchDbRepositorySupport its handled type will also examined for * annotations eligible for view generation. * * @param repository * @return a Map with generated views. */ public Map<String, DesignDocument.View> generateViews(final Object repository) { final Map<String, DesignDocument.View> views = new HashMap<String, DesignDocument.View>(); final Class<?> repositoryClass = repository.getClass(); final Class<?> handledType = repository instanceof CouchDbRepositorySupport<?> ? ((CouchDbRepositorySupport<?>) repository).getHandledType() : null; createDeclaredViews(views, repositoryClass); eachMethod( repositoryClass, new Predicate<Method>() { public boolean apply(Method input) { if (hasAnnotation(input, GenerateView.class)) { generateView(views, input, handledType); } return false; } }); if (handledType != null) { views.putAll(generateViewsFromPersistentType(handledType)); } return views; }
private void generateView( Map<String, DesignDocument.View> views, Method me, Class<?> handledType) { String name = me.getName(); if (!name.startsWith("findBy") && !name.equals("getAll")) { throw new ViewGenerationException( String.format( "The method: %s in %s annotated with GenerateView does not conform to the naming convention of 'findByXxxx'", name, me.getDeclaringClass())); } Class<?> type = resolveReturnType(me); if (type == null) { if (handledType != null) { type = handledType; } else { throw new ViewGenerationException( "Could not resolve return type for method: %s in %s", me.getName(), me.getDeclaringClass()); } } String typeDiscriminator = resolveTypeDiscriminator(type); if (name.equals("getAll")) { if (typeDiscriminator.length() < 1) { throw new ViewGenerationException( String.format( "Cannot generate 'all' view for %s. No type discriminator could be resolved. Try annotate unique field(s) with @TypeDiscriminator", type.getDeclaringClass())); } views.put("all", generateAllView(typeDiscriminator)); return; } String finderName = name.substring(6); String fieldName = resolveFieldName(me, finderName); Method getter = findMethod(type, "get" + fieldName); if (getter == null) { // try pluralis fieldName += "s"; getter = findMethod(type, "get" + fieldName); } if (getter == null) { throw new ViewGenerationException( "Could not generate view for method %s. No get method found for property %s in %s", name, name.substring(6), type); } fieldName = firstCharToLowerCase(fieldName); DesignDocument.View view; if (isIterable(getter.getReturnType())) { view = generateFindByIterableView(fieldName, typeDiscriminator); } else { view = generateFindByView(fieldName, typeDiscriminator); } views.put("by_" + firstCharToLowerCase(finderName), view); }
private void addView( Map<String, DesignDocument.View> views, View input, Class<?> repositoryClass) { if (input.file().length() > 0) { views.put(input.name(), loadViewFromFile(views, input, repositoryClass)); } else { views.put(input.name(), DesignDocument.View.of(input)); } }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ViewQuery other = (ViewQuery) obj; if (cachedQuery == null) { if (other.cachedQuery != null) return false; } else if (!cachedQuery.equals(other.cachedQuery)) return false; if (dbPath == null) { if (other.dbPath != null) return false; } else if (!dbPath.equals(other.dbPath)) return false; if (descending != other.descending) return false; if (designDocId == null) { if (other.designDocId != null) return false; } else if (!designDocId.equals(other.designDocId)) return false; if (endDocId == null) { if (other.endDocId != null) return false; } else if (!endDocId.equals(other.endDocId)) return false; if (endKey == null) { if (other.endKey != null) return false; } else if (!endKey.equals(other.endKey)) return false; if (group != other.group) return false; if (groupLevel != other.groupLevel) return false; if (ignoreNotFound != other.ignoreNotFound) return false; if (includeDocs != other.includeDocs) return false; if (inclusiveEnd != other.inclusiveEnd) return false; if (key == null) { if (other.key != null) return false; } else if (!key.equals(other.key)) return false; if (limit != other.limit) return false; if (listName == null) { if (other.listName != null) return false; } else if (!listName.equals(other.listName)) return false; if (queryParams == null) { if (other.queryParams != null) return false; } else if (!queryParams.equals(other.queryParams)) return false; if (reduce != other.reduce) return false; if (skip != other.skip) return false; if (staleOk == null) { if (other.staleOk != null) return false; } else if (!staleOk.equals(other.staleOk)) return false; if (startDocId == null) { if (other.startDocId != null) return false; } else if (!startDocId.equals(other.startDocId)) return false; if (startKey == null) { if (other.startKey != null) return false; } else if (!startKey.equals(other.startKey)) return false; if (viewName == null) { if (other.viewName != null) return false; } else if (!viewName.equals(other.viewName)) return false; return true; }
private void generateSetBasedDocRefView( Map<String, org.ektorp.support.DesignDocument.View> views, Member me, DocumentReferences referenceMetaData) { String fieldName = firstCharToLowerCase(me.getName()); String orderBy = referenceMetaData.orderBy(); String backRef = referenceMetaData.backReference(); if (backRef.length() == 0) { throw new ViewGenerationException( String.format( "The DocumentReferences annotation in %s must specify a backReference", me.getDeclaringClass())); } String viewName = NameConventions.backReferenceViewName(fieldName); String typeDiscriminator = resolveTypeDiscriminatorForBackReference(me); if (orderBy.length() > 0) { views.put( viewName, generateDocRefsAsSetWithOrderByView(backRef, fieldName, orderBy, typeDiscriminator)); } else { views.put(viewName, generateDocRefsAsSetView(backRef, fieldName, typeDiscriminator)); } }
@SuppressWarnings("unchecked") @Override public String success(HttpResponse hr) throws Exception { Map<String, ?> rsp = objectMapper.readValue(hr.getContent(), Map.class); return (String) rsp.get(REVISION_FIELD_NAME); }
private void appendQueryParams(URI query) { for (Map.Entry<String, String> param : queryParams.entrySet()) { query.param(param.getKey(), param.getValue()); } }
public String buildQuery() { if (cachedQuery != null) { return cachedQuery; } URI query = buildViewPath(); if (isNotEmpty(key)) { query.param("key", key); } if (isNotEmpty(startKey)) { query.param("startkey", startKey); } if (isNotEmpty(endKey)) { query.param("endkey", endKey); } if (isNotEmpty(startDocId)) { query.param("startkey_docid", startDocId); } if (isNotEmpty(endDocId)) { query.param("endkey_docid", endDocId); } if (hasValue(limit)) { query.param("limit", limit); } if (staleOk != null) { query.param("stale", staleOk); } if (descending) { query.param("descending", "true"); } if (!inclusiveEnd) { query.param("inclusive_end", "false"); } if (!reduce) { query.param("reduce", "false"); } if (hasValue(skip)) { query.param("skip", skip); } if (includeDocs) { query.param("include_docs", "true"); } if (group) { query.param("group", "true"); } if (hasValue(groupLevel)) { query.param("group_level", groupLevel); } if (queryParams != null && !queryParams.isEmpty()) { appendQueryParams(query); } cachedQuery = query.toString(); return cachedQuery; }
public ViewQuery queryParam(String name, String value) { queryParams.put(name, value); return this; }