MVC 4 return entries from database excluding specific items - database

I've been changing this call around for some time and can not quite get it to work. I am trying to return some records from a database and exclude items based on their ID numbers.
This is a database of quotes and when someone hides a quote it stores the quoteID in a cookie (required) and when returning to the site, The cookie.value is read.
I need to filter these IDs out of the return query and display the results in a view using WebGrid. I have the cookie part working. I retrieve the values (could be multiple) and split this into a list of strings. I then run a foreach loop ans parse the string to an integer and then try to remove quotes that I have retrieved from the database. Here is the code as I am using it...
// Filter on Cookie Value using tokenizer
string value = Request.Cookies.Get("hideCookie").Value;
List<string> values = value.Split(' ').ToList();
var quotes2 = db.Quotes.Include(q => q.QName);
foreach (var i in values)
{
int idv = int.Parse(i);
quotes2 = from q in quotes2 where q.QuoteID != idv select q;
}
return View(quotes2.ToList());
This throws an error "A specified Include path is not valid. The EntityType 'Exercise4.Models.Quote' does not declare a navigation property with the name 'QName'." (QName being an entry in the database record).
If I remove the ToList() in the return View(), quotes2 gets passed to the View but throws this error on the webGrid "A specified Include path is not valid. The EntityType 'Exercise4.Models.Quote' does not declare a navigation property with the name 'QName'."
I'm sure there is a much better way of doing this. Can you please point me in the correct direction so I can stop banging my head against my desk...

Related

Path not included in Queryresult (row.getValue)

we still have Jackrabbit 2 (2.18.0) in use and I have a problem with loading the "jcr:path" in the Query-Result.
In this example we have a custom node "org:permission" with a custom property "org:permissionHolderIds".
The custom property is filled in values in the found rows.
String queryString = "SELECT [jcr:path], [org:permissionHolderIds] FROM [org:permission]";
QueryManager queryManager = session.getWorkspace().getQueryManager();
Query query = queryManager.createQuery(queryString, Query.JCR_SQL2);
QueryResult queryResult = query.execute();
RowIterator rows = queryResult.getRows();
Row r = rows.nextRow();
String holder= r.getValue("org:permissionHolderIds").getString();
// is filled properly
String path = r.getValue("jcr:path").getString();
// path is empty all the time :-(
Until now we iterated over the found nodes but this causes additional data base queries and is slow. So we tried to improve our performance and fetch all the necessary data in the query.
Thank you!
jcr:path is not a node property, so this is not supposed to do what you want. That said, why don't you use https://docs.adobe.com/docs/en/spec/javax.jcr/javadocs/jcr-2.0/javax/jcr/query/Row.html#getPath()?

Getting null value when accessing fields from result in Searcher in Vespa

My Searcher is given below. When I hit search API then in my Searcher, I am getting null values when I tried to get fields of document. I am able to get id, relavance and source but not fields. Why is it so?Am I doing something wrong? Please help.
#Override
public Result search(Query query, Execution execution) {
// pass it down the chain to get a result
Result result = execution.search(query);
String title = result.hits().get(0).getField("title");
System.out.println(title);
// return the result up the chain
return result;
}
I am getting null value in title.
This is because results are initially surfaced without field data added (for performance).
Add
execution.fill(result);
before accessing fields (send the summary class to fill as second argument if you have multiple ones).
Use execution.fill(result) to fill the result before accessing fields. See also https://docs.vespa.ai/documentation/reference/inspecting-structured-data.html

Can someone help me add a new column in Google cloud Search Index using Java

I tried adding a new column to an existing search index but it is throwing an error, the error as stated below:-
Field docname is present 0 times; expected 1
java.lang.IllegalArgumentException: Field docname is present 0 times; expected 1
I can see that the new column has been added to the search index but cannot retrieve the index.
By my observation i can see that the existing records in the index dont have the new column data and hence it is giving a this error, but the new records will be having this column values. Can anyone help me with this.
Upon having this problem myself today, I searched a bit in the documentation. It was a rather frustrating error as it didn't actually pin point where the problem was in my code.
It appears that when you use getOnlyField("something") on a Document (in this case on one of many returned from a search query), if that field does not actually exist yet in that specific document it throws the java.lang.IllegalArgumentException.
Because that can often be the case when you update an index with new columns, I'm using something like this to get around it:
public static Long getNumberField(ScoredDocument d, String name, Long defaultValue) {
try {
return d.getOnlyField(name).getNumber().longValue();
} catch (IllegalArgumentException e) {
return defaultValue;
}
}
Which is called in the search results code:
Long numberValue = SearchUtils.getNumberField(scoredDocument, "featuredOrder", -1L)
This allows me to catch that error and return a default value when it doesn't exist.
You can find the documentation here:
https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/search/Document.html#getOnlyField-java.lang.String-

How do I remove a member from a large ldap-ad group with over >1500 members

I'm trying to remove a member from a large ldap Active Directory (AD) group. The below code will remove the member if the group is small. However it won't work if it's larger since AD splits the members into multiple range related attributes.
group.removeMember(person.getFullDn());
ldapTemplate.update(group);
I've tried to access those attributes directly using something like the below. IncrementalAttributesMapper allows me to get the list of range related member attributes ie member;Range=0-1499 and I attempt to delete the person from each, but no good. I don't get an error, but the person isn't removed from the group either
DirContextOperations ctx = ldapTemplate.lookupContext(group.getDn());
IncrementalAttributesMapper<?> attributesMapper = new DefaultIncrementalAttributesMapper("member");
while (attributesMapper.hasMore()) {
String[] attributes = attributesMapper.getAttributesForLookup();
for (String attribute: attributes ) {
ldapTemplate.lookup(group.getDn(), attributesMapper.getAttributesForLookup(), attributesMapper);
ctx.removeAttributeValue(attribute, person.getDn() );
ldapTemplate.modifyAttributes(ctx);
}
}
Hopefully someone has had more success with this. Thanks in advance!
I've got the solution as posted
I was getting a malformed attribute error until I just realised that the BasicAttribute is expecting String parameters. I was incorrectly passing in a Name object to it.
ldapTemplate.modifyAttributes(group.getDn(), new ModificationItem[] {
new ModificationItem(
DirContext.REMOVE_ATTRIBUTE,
new BasicAttribute("member", person.getFullDn().toString() ))
});

Why does this get method stop returning correctly?

I am trying to write an app engine application for my university. What I am trying to achieve right now, is to create a method which takes in a Course name, and returns a list of all the CourseYears (think of that as being like a link table e.g. if Maths is the course, and it has Year 1, year 2 and Year 3; MathsYear1, MathsYear2 and MathsYear3 would be the names of the CourseYears).
This is the code for the module (WARING: super dirty code below!):
#ApiMethod(name = "courseYears")
public ArrayList<CourseYear> courseYears(#Named("name") String name){
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query.Filter keyFilter = new Query.FilterPredicate("name", Query.FilterOperator.EQUAL, name);
Query query = new Query("Course").setFilter(keyFilter);
PreparedQuery preparedQuery = datastore.prepare(query);
List<Entity> resultList = preparedQuery.asList(FetchOptions.Builder.withLimit(1));
Course course = ofy().load().type(Course.class).id(resultList.get(0).getKey().getId()).now();
ArrayList<String> courseYearNames = course.getAllCourseYearNames();
System.out.println(course.getName());
ArrayList<CourseYear> courseYears = new ArrayList<CourseYear>();
for(String courseYearName: courseYearNames){
Query.Filter courseNameFilter = new Query.FilterPredicate("name", Query.FilterOperator.EQUAL, courseYearName);
Query query2 = new Query("CourseYear").setFilter(courseNameFilter);
List<Entity> resL = preparedQuery.asList(FetchOptions.Builder.withLimit(1));
System.out.println("test");
CourseYear courseYear = ofy().load().type(CourseYear.class).id(resL.get(0).getKey().getId()).now();
courseYears.add(courseYear);
}
return courseYears;
}
It basically takes a Course name in, applies a filter on all courses to get the corresponding Course object, and then calls getAllCourseYearNames() on the course to get an array list containing all its CourseYears' names. (I would have loved to do this using Keys, but parameterised Objectify keys don't seem to be supported in this version of App Engine).
I then try and get the CourseYears by looping through the arraylist of names and applying the filter for each name. I print "test" each time to see how many times it is looping. Like I said, a super dirty way of doing it.
When I try passing a few course names as a parameters, it loops the correct number of times only once or twice, and after that does not loop at all (doesn't print "test"). I could understand if it never looped, but not doing it correctly once or twice and then never again. It doesn't successfully return a list of CourseYears when it does work, but rather the relevant number of NULLs - I don't know if this is relevant. I believe it successfully retrieves the course every time, as I print the name of the course after loading and it never fails to do this.
If anyone has ANY suggestions for why this may be happening, I would be incredibly grateful to hear them!
Thanks
query2 is never used in your code. You reuse preparedQuery from your previous query, which runs on a different entity kind.

Resources