Parse: findInBackground only returns results for currentUser - parse-platform

I've searched and searched with no luck.
FYI: I'm a newbie...I'm trying to query a class for non-current user data. I've verified that user_objectID and userID are correct and that Parse has these files, but the list shows up as 0. When I switch userID to my ID then it works.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Workouts");
query.whereEqualTo("user_objectID", userID);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
Log.v(TAG, userID + ": " + list.size());
for (ParseObject each : list) {
Log.v(TAG, each.getDouble("distance") + "");
}
} else {
Log.v(TAG, e.getLocalizedMessage());
}
}
});
What am I doing wrong? Is it a setting? Am I not using the right query paramters?
Thanks so much for any insight.
Tyler

Related

Getting the list of unanswered quizz by a player

I am working on a small quizz application to learn Parse.
In terms of data I have a table Quiz, a table Player and a table QuizAnswered. The table QuizAnswered contains a list of Quiz where a Player has taken part. So it is a many to many relationship.
I would like to get the list of Quiz that a player has NOT taken part in.
I have been able to get it but I need to do two api calls and I wondering if there is any better way to do it. Here is the code I am using I am not very proud of it to be frank.
public void getUnansweredQuizListForPlayer(Player p) {
ParseQuery<ParseObject> quizAnsweredQuery = ParseQuery.getQuery("QuizAnswered");
quizAnsweredQuery.whereEqualTo("player", p);
quizAnsweredQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(final List<ParseObject> quizAnsweredList, ParseException e) {
if (e == null) {
ParseQuery<Quiz> quizQuery = ParseQuery.getQuery("Quiz");
quizQuery.findInBackground(new FindCallback<Quiz>() {
public void done(List<Quiz> quizList, ParseException e) {
if (e == null) {
ArrayList<Quiz> quizzes = new ArrayList<Quiz>();
for (Quiz quiz : quizList) {
boolean answered = false;
for (ParseObject o :quizAnsweredList) {
Quiz quizAnswered = (Quiz) o.getParseObject("quiz");
if (quizAnswered.getObjectId().equals(quiz.getObjectId())) {
answered = true;
}
}
if (!answered) {
quizzes.add(quiz);
}
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
Is there a way to get the list of not participated quiz for a player in only one Query ?
Thank you in advance for the help.
You could store objectId of Quiz in Player's answeredQuizList column, which will be array containing objectId's of quizes which the user gave answer to.
And then make Quiz query with: (pseudo-code)
query whereKey:"objectId" notContainedIn: user.answeredQuizList

Geocoder.getFromLocationName does not work for intersection address

I am trying to use Geocoder class in my App and found that Geocoder.getFromLocationName returns null if the address is intersection of two streets. Ex. "Quail Ave and Dunford Way, Sunnyvale, CA 94087" . However, the method works perfect for any other type address.
During Debug session, I could see the address parameter passed and reflects correctly, but "geocodeMatches" returns size(0) zero.
Appreciate any Help!!
public static List<Address> getGeoCode(#NonNull Context context, #NonNull String address){
try {
geocodeMatches = new Geocoder(context).getFromLocationName(address, 1);
if (!geocodeMatches.isEmpty())
{
return geocodeMatches;
}
} catch (IOException e) {
Log.e("ERROR", "Error to retrieve geocode" + e);
e.printStackTrace();
}
return geocodeMatches;
}
GeoCoder is working on NetworkLocationService. In some cases, NetworkLocationService is stopped working due to some reason. I don't know exact reason. But you can get address from google api in that case.
Hope below code will help you!
public static String GetLocationAddressFromLatLong(Context context, double latitude, double longitude) {
String finalAddress = "";
Geocoder geocoder;
List<Address> addresses = null;
geocoder = new Geocoder(context.getApplicationContext(), Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
finalAddress = addresses.get(0).getAddressLine(0) + ", " + addresses.get(0).getAddressLine(1);
} else {
//Get address from Google api
try {
JSONObject jsonObj = getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + ","
+ longitude + "&sensor=true");
String Status = jsonObj.getString("status");
if (Status.equalsIgnoreCase("OK")) {
JSONArray Results = jsonObj.getJSONArray("results");
JSONObject location = Results.getJSONObject(0);
finalAddress = location.getString("formatted_address");
}
} catch (Exception e) {
e.printStackTrace();
}
}
return finalAddress;
}

What is the best way to get the max id of table?

What is the best way to get the max id of table? Below I have paste the error and code. So I was planning on using afterLast() method to get the max id but I get an error.
ERROR:
SQLException: feature not supported
Code:
public class ex03 {
public static void main(String[] args) {
String url = "jdbc:ucanaccess://C:/Users/dave_000/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/T_01_JDBC_01.accdb";
Connection con;
// Get Max ID
Statement stmt0;
String query0 = "select * from user";
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url, "", "");
stmt0 = con.createStatement();
// Get last ID
ResultSet rs = stmt0.executeQuery(query0);
rs.afterLast();
int maxID = rs.getInt("ID");
System.out.println(maxID);
pstmt1.close();
con.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
It is much more efficient to use SQL to find the maximum value:
select max(id) from user

spring security's searchForSingleEntryInternal method throws exception if record not found

I'm working on an application that uses Spring Security's searchForSingleEntryInternal method. Is there a way to do the same thing without throwing an exception if a record is not found? I want to be able to create a condition that handles missing records.
What I want to change
if (results.size() == 0) {
throw new IncorrectResultSizeDataAccessException(1, 0);
}
From this method
/**
* Internal method extracted to avoid code duplication in AD search.
*/
public static DirContextOperations searchForSingleEntryInternal(DirContext ctx, SearchControls searchControls,
String base, String filter, Object[] params) throws NamingException {
final DistinguishedName ctxBaseDn = new DistinguishedName(ctx.getNameInNamespace());
final DistinguishedName searchBaseDn = new DistinguishedName(base);
final NamingEnumeration<SearchResult> resultsEnum = ctx.search(searchBaseDn, filter, params, searchControls);
if (logger.isDebugEnabled()) {
logger.debug("Searching for entry under DN '" + ctxBaseDn + "', base = '" + searchBaseDn + "', filter = '" + filter + "'");
}
Set<DirContextOperations> results = new HashSet<DirContextOperations>();
try {
while (resultsEnum.hasMore()) {
SearchResult searchResult = resultsEnum.next();
// Work out the DN of the matched entry
DistinguishedName dn = new DistinguishedName(new CompositeName(searchResult.getName()));
if (base.length() > 0) {
dn.prepend(searchBaseDn);
}
if (logger.isDebugEnabled()) {
logger.debug("Found DN: " + dn);
}
results.add(new DirContextAdapter(searchResult.getAttributes(), dn, ctxBaseDn));
}
} catch (PartialResultException e) {
LdapUtils.closeEnumeration(resultsEnum);
logger.info("Ignoring PartialResultException");
}
if (results.size() == 0) {
throw new IncorrectResultSizeDataAccessException(1, 0);
}
if (results.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1, results.size());
}
return results.iterator().next();
}
}
I'm somewhat new to spring and maybe I'm missing something obvious. Any advice would be much appreciated
easy fix, just had to copy over the searchForSingleEntryInternal method from Spring Security and place it in my own project. From there I was able to tweak the exception handling so the application didn't come to a grinding halt if a record wasn't found.

Sorting item from JComboBox

I'm having problem sorting my items from jcombobox, here are my codes.
public void fillCombo()
{
String dataSourceName = "CheckWriterDB";
String dbURL = "jdbc:odbc:" + dataSourceName;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(dbURL, "", "");
st = con.createStatement();
st.execute("select Suppliers from SuppliersTable");
rs = st.getResultSet();
if(rs!=null)
{
while(rs.next())
{
temp = rs.getString(1);
listOfSuppliersCombo.addItem(temp1);
}
}
st.close();
con.close();
}
catch(Exception e)
{
System.out.println("Your error is: " + e);
}
}
Can anybody help me on how to sort the item shown in my JComboBox, data source of the items shown in my combobox is from my DATABASE. Thank you so much.
Use order by in your query to retrieve data ordered from your database
st.execute("select Suppliers from SuppliersTable order by <fields>");
Syntax: http://en.wikipedia.org/wiki/Order_by_(SQL)

Resources