I've been working on a searching auto-complete feature with Bootstrap-3-Typeahead ,GWT and data comes from http://dawa.aws.dk/dok/api by JsonP request..
The Typahead creation method is below
private Typeahead<Location> createTypeAhead() {
typeAhead = new Typeahead<>(new Dataset<Location>() {
#Override
public void findMatches(final String query, final SuggestionCallback<Location> callback) {
requestCounter--;
startSendingRequest = true;
clear.setIcon(IconType.SPINNER);
clear.setIconSpin(true);
final Set<Suggestion<Location>> suggestions = new HashSet<>();
queryLower = query.toLowerCase();
JsonpRequestBuilder jsonpRequestBuilder;
if (!streetSelected) {
jsonpRequestBuilder = new JsonpRequestBuilder();
jsonpRequestBuilder.requestObject("https://dawa.aws.dk/vejnavne/autocomplete?side=1&per_side=500&noformat=1&q=" + queryLower + "*", new AsyncCallback<MyJsArray<VejAutocomplete>>() {
#Override
public void onFailure(Throwable caught) {
Notify.notify("suggestion matches failed");
}
#Override
public void onSuccess(MyJsArray<VejAutocomplete> result) {
Set<Location> locationSet = new LinkedHashSet<>();
for (VejAutocomplete item : result.getAsList()) {
String lowerCase = item.getTekst().toLowerCase();
if (lowerCase.startsWith(queryLower)) {
locationSet.add(new Location(Location.LocationType.STREET, item.getTekst(), item));
locationArrayList.clear();
locationArrayList.addAll(locationSet);
}
}
}
});
}
for (Location address : locationArrayList) {
String value = address.getValue();
Suggestion<Location> s = Suggestion.create(value, address, this);
if (address.getValue().toLowerCase().startsWith(queryLower)) {
suggestions.add(s);
}
}
callback.execute(suggestions);
if (typeAhead.getValue().length() != 0 && queryLower.length() <= 5 && requestCounter < 5 && requestCounter > 0) {
new Timer() {
#Override
public void run() {
findMatches(queryLower, callback);
}
}.schedule(500);
} else {
clear.setIconSpin(false);
clear.setIcon(IconType.CLOSE);
requestCounter = 5;
}
}
});
return typeAhead;
}
The result is like below:
I used recursion to send 4-5 times of request because it's not showing the suggestion list with the keyword of single letter. And It still won't work with some single letters like "s" or "e". Data successfully retrieved from the API but doesn't show in the suggestion list like below:
I assume that I should cache all search results then recreate the auto-complete from scratch, it becomes complicated in that case.
Any good idea to solve this problem?
Related
I have this class ( DoctorRegistrationTest.java ) and having the global variable doctorCode , doctorFirstName , doctorFamilyName )
#BeforeMethod(groups = {"BVT", "Regression"})
public void loadRmsPage() {
loadRmsProfile();
softAssert = new SoftAssert();
doctorRegistrationPage = new DoctorRegistrationPage(webDriver);
}
#Epic("RMS - Create Doctor")
#Feature("RMS - Create Doctor functionality")
#Story("AUT-688")
#Severity(SeverityLevel.BLOCKER)
#Test(description = "Positive : Doctor Creation ", groups = {"BVT", "Regression"})
public void createDoctorTestMethod() {
do {
doctorCode = String.valueOf(generateRandomNumber(1000, 9999));
} while (false && doctorRegistrationPage.verifyCreatedDoctor(doctorCode));
setDoctorRegistrationInformation();
createDoctor();
}
public void createDoctor() {
doctorRegistrationPage.loadDoctorRegistrationPage();
doctorRegistrationPage.fillDoctorNameInformation(doctorCode, doctorFirstName, doctorFamilyName, doctorFirstNameInArabic, doctorFamilyNameInArabic);
doctorRegistrationPage.fillDoctorGeneralInformation(dateOfBirth, joinDate, identityNo, expiryDate, gender, doctorTitle, employmentType, identityType);
doctorRegistrationPage.fillDoctorContactInformation(mobileNumber, companyEmail);
doctorRegistrationPage.fillDoctorOtherInformation(doctorInformationEnglish, doctorInformationArabic);
doctorRegistrationPage.fillDoctorTiming(followUpDays, slotDurationMinutes);
doctorRegistrationPage.fillDoctorHospitalsAndClinics(hospital, clinics);
doctorRegistrationPage.fillDoctorDefaultProcedure(defaultProcedur, consultationProcedure);
boolean result = doctorRegistrationPage.verifyCreatedDoctor(doctorCode);
LOG.info("Return value of verifyCreatedDoctor method " + result);
softAssert.assertTrue(result, "TEST FAILED - CREATED DOCTOR NOT LISTING IN THE GRID");
softAssert.assertAll();
doctorRegistrationPage.logOutUser();
}
public void setDoctorRegistrationInformation() {
readPropertiesFile();
setNames();
setNumberValues();
setDate();
}
public void setNames() {
doctorFirstName = "AUT DN " + getRandomStringName(4);
doctorFamilyName = "AUT DFN " + getRandomStringName(4);
companyEmail = getRandomStringName(5) + "#aut.com";
doctorInformationEnglish = getRandomStringName(6);
}
public String getRandomStringName(int randomStringLength) {
return RandomStringUtils.randomAlphabetic(randomStringLength).toLowerCase();
}
public int generateRandomNumber(int low, int high) {
return (new Random()).nextInt(high - low) + low;
}
}
I have another class ( DoctorScheduleTest.java ) and needs to pass doctorCode , doctorFirstName , doctorFamilyName from (DoctorRegistrationTest.java ) to doctorSchedulePage.selectDoctorDropdown(doctor); method, instead of hardcoding the detail. Must pass the value like this (" doctorCode: doctorFirstName doctorFamilyName )
private String doctor =" 8938: AUT DN gvgl AUT DFN wnrn ";
#BeforeMethod(groups = {"BVT", "Regression"})
public void loadRmsPage()
{
loadRmsProfile();
softAssert = new SoftAssert();
doctorSchedulePage = new DoctorSchedulePage(webDriver);
}
#Epic("RMS")
#Feature("RMS - Create Doctor Schedule functionality")
#Story("AUT-835")
#Severity(SeverityLevel.BLOCKER)
#Test(description = "Positive : Doctor Schedule Creation", groups = {"BVT", "Regression"})
public void doctorScheduleCreationTestMethod()
{
setTemplateName();
createInitialSchedule();
setSchedule();
saveTemplate();
setTemplate();
setDateRange();
generateSchedule();
}
public void createInitialSchedule()
{
doctorSchedulePage.loadDoctorScheduleDashboard();
doctorSchedulePage.selectHospitalDropDown(hospital);
doctorSchedulePage.selectClinicDropDown(clinic);
doctorSchedulePage.selectDoctorDropdown(doctor);
doctorSchedulePage.fillTemplate(scheduleTemplateName);
}
public void setSchedule()
{
doctorSchedulePage.sundaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.mondaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.tuesdaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.wednesdaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.thursdaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.fridaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
doctorSchedulePage.saturdaySchedule(startHourTime,startMinuteTime,startSchedule,endHourTime,endMinuteTime,endSchedule);
}
public void saveTemplate()
{
doctorSchedulePage.saveTemplate();
}
public void setTemplate()
{
doctorSchedulePage.selectTemplateDropdown(scheduleTemplateName);
}
public void setDateRange()
{
doctorSchedulePage.selectScheduleDate(scheduleStartDate,scheduleEndDate);
}
public void generateSchedule()
{
doctorSchedulePage.generateSchedule();
}
public int generateRandomNumber(int low, int high)
{
return (new Random()).nextInt(high - low) + low;
}
public void setTemplateName()
{
scheduleTemplateName = "Template " + getRandomStringName(3);
}
public String getRandomStringName(int randomStringLength)
{
return RandomStringUtils.randomAlphabetic(randomStringLength).toLowerCase();
}
}
DoctorSchedulePage.java ( selectDoctorDropdown method )
public void selectDoctorDropdown(String doctorcode)
{
selectValueFromDropDown(doctorDropdown, doctorcode);
}
BasePage.java (selectValueFromDropDown method )
protected void selectValueFromDropDown(WebElement element, String value) {
if (value != null && !value.isEmpty()) {
waitForElementToPresent(element);
Select dropdown = new Select(element);
LOG.info("Selected "+value);
dropdown.selectByVisibleText(value);
}
}
First of all, your question indicates, that you have an issue with passing a test data into tests. That should be addressed in the first place.
Since that is another topic (and a much bigger one) I would recommend a workaround, using a separate class, with a doctor's data.
public static class Doctor{
public static String FirstName;
public static String FamilyName;
Initialize the data in the setup and use is whenever you need.
As I said before, that is not the best solution, as testing data should be kept outside of the tests, but might be helpful for now.
I am trying to create an indoor location app to detect own location and wayfinding. I have already set up my bluetooth app to scan and display found devices and beacons. However i found out that some of the devices found were repeated. What do i need to do to stop it and i wanted to find only certain devices mac address. May i know how to use the method ScanFilter and what is the difference between ScanFilter and ScanFilter.Builder? Do i need to implement ScanSetting?
i have try putting this
ScanFilter scanFilterMac = new ScanFilter.Builder().setDeviceAddress("88:88:88:B0:03:DB").build();
I change the mac address only but didn't get any result. and can i do something like this since i know the first 6 digit is the same
ScanFilter scanFilterMac = new ScanFilter.Builder().setDeviceAddress("68:54:F5:([A-Fa-f0-9]{2}:){2}([A-Fa-
f0-9]{2})").build();
public void startScanning() {
final BluetoothLeScanner btScanner = btAdapter.getBluetoothLeScanner();
if (btScanner == null) {
// not enabled yet or not supported
return;
}
System.out.println("start scanning");
peripheralTextView.setText("");
startScanningButton.setVisibility(View.INVISIBLE);
stopScanningButton.setVisibility(View.VISIBLE);
AsyncTask.execute(new Runnable() {
#Override
public void run() {
ScanFilter scanFilterMac = new ScanFilter.Builder().setDeviceAddress("68:54:F5:88:88:88").build();
listFilter.add(scanFilterMac);
btScanner.startScan(listFilter,leScanCallback);
}
});
}
final class BleScanCallback extends ScanCallback {
// address -> device map
private final Map<String, BluetoothDevice> scanResults;
public void onScanResult(int callbackType, ScanResult result) {
this.addScanResult(result);
}
public void onBatchScanResults( List results) {
for (final Object result1 : results) {
ScanResult result = (ScanResult) result1;
this.addScanResult(result);
}
}
public void onScanFailed(int errorCode) {
}
private void addScanResult(ScanResult result) {
BluetoothDevice device = result.getDevice();
String name = device.getName();
if (name != null && !this.scanResults.containsKey(device.getAddress())) {
String var6 = device.getAddress();
this.scanResults.put(var6, device);
} else {
StringBuilder var10001 = (new StringBuilder()).append("Ble addScanResult ignore ");
Log.d("Ble", var10001.append(device.getName()).append(' ').append(device.getType()).append(' ').append(device.getAddress()).append(' ').toString());
}
}
public BleScanCallback( Map<String, BluetoothDevice> scanResults) {
super();
this.scanResults = scanResults;
}
}
I have a method for get a List of users by its uid:
public void getNeededUsers(List<String> uidList, UsersListCallback usersListCallback) {
CollectionReference users = db.collection(Consts.USERS_DB);
for (String uid: uidList) {
users.whereEqualTo(Consts.UID, uid).get()
.addOnCompleteListener(task -> {
List<FsUser> fsUserList = new ArrayList<>();
for (DocumentSnapshot snapshot : task.getResult().getDocuments()) {
fsUserList.add(snapshot.toObject(FsUser.class));
}
usersListCallback.getUsers(fsUserList);
});
}
}
But it seems that this method doesn`t work properly. When I try to iterate through the List of users in another method, I receive users one by one from callback, instead of getting all, so they are duplicated in my list.
Here is a code of another method:
public void createUserChats(UserChatListCallback userChatListCallback) {
final List<UserChat> userChatList = new ArrayList<>();
getChatAndUidList((chatList, uidList) -> {
getRtUsersAndMessages(uidList, (rtUserList, messageList) -> {
getNeededUsers(uidList, userList -> {
if (!userList.isEmpty()) {
for (int i = 0; i < userList.size(); i++) {
String name = userList.get(i).getName();
String image = userList.get(i).getImage();
String userId = userList.get(i).getUid();
long timeStamp = chatList.get(i).getTimeStamp();
boolean seen = chatList.get(i).getSeen();
String online = rtUserList.get(i).getOnline();
String message = messageList.get(i).getMessage();
long messageTimestamp = messageList.get(i).getTime();
userChatList.add(new UserChat(name, image, timeStamp, seen, userId, online,
message, messageTimestamp));
}
}
userChatListCallback.getUserChatList(userChatList);
});
});
});
}
To get a list of users, please use the following code:
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (String uid: uidList) {
Task<DocumentSnapshot> documentSnapshotTask = users.whereEqualTo(Consts.UID, uid).get();
tasks.add(documentSnapshotTas);
}
Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
#Override
public void onSuccess(List<Object> list) {
//Do what you need to do with your users list
for (Object object : list) {
FsUser fsUser = ((DocumentSnapshot) object).toObject(TeacherPojo.class);
Log.d("TAG", fsUser.getName());
}
}
});
I am trying to avoid the asynchronous layout pefromaed naturally with Firebase. so I am trying to complete my method in the one dataonchanged method.
I need to know is it possible and how i can reference 2 nodes as you can see , using the one reference. iv tried something like
dataBaseLastIn = FirebaseDatabase.getInstance().getReference("clockInDate" + "clockOutDate");
//and
dataBaseLastIn = FirebaseDatabase.getInstance().getReference("clockInDate");
dataBaseLastIn = FirebaseDatabase.getInstance().getReference("clockOutDate");
//Method that works to set text to one reference but not both
protected void onStart() {
super.onStart();
final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
dataBaseLastIn.addValueEventListener(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dateinList.clear();
dateOutList.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Date date = postSnapshot.getValue(Date.class);
String datt = date.getDate().toString().trim();
String user = date.getCurrUser().toString().trim();
if (DateNew.equals(datt) && user.equals(uid)) {
dateinList.add(date);
} else {
dateinList.remove(date);
}
DateOut dateOut = postSnapshot.getValue(DateOut.class);
String dattOut = dateOut.getDate().toString().trim();
String userOut = dateOut.getCurrUser().toString().trim();
if (DateNew.equals(dattOut) && userOut.equals(uid)) {
dateOutList.add(dateOut);
} else {
dateOutList.remove(date);
}
}
newList = dateinList;
outList = dateOutList;
try {
totalINList();
} catch (ParseException e) {
e.printStackTrace();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
//setting the text
public void totalINList() throws ParseException {
for(int i = 0 ; i < newList.size(); i++) {
timeDuration.add(newList.get(i).getClockTime());
}
Date e = newList.get(newList.size() - 1);
String timeIn = e.getClockTime();
txtCurrentTime.setText(timeIn);
for(int i = 0 ; i < outList.size(); i++) {
timeDurationOut.add(outList.get(i).getTime());
}
DateOut f = outList.get(outList.size() - 1);
String timeOut = f.getTime();
txtCurrentTimeOut.setText(timeOut);
}
May be something simple but i only get the value of the last reference i declare which obviously makes sense but was wondering is ther a way i can reference both nodes in the one
No, you cannot achieve something like with Firebase.
The initial response for most developers is to try and "fix" this asynchronous behavior, which I personally recommend against this. The web is asynchronous, and the sooner you accept that, the sooner you can learn how to become productive with modern web APIs.
I Have a multithreaded environment in android app. I use a singleton class to store data. This singleton class contains a arraylist that is accessed using a synchronized method.
The app uses this arraylist to render images in app.
Initial problem : Concurrent modification error use to come so I made the get arraylist function syncronized.
Current Problem:Concurrent modification error not coming but in between empty arraylist returned (maybe when there is concurrent access).
Objective : I want to detect when Concurrent modification so that Instead of empty arraylist being return I can return last state of the arraylist.
public synchronized List<FrameData> getCurrentDataToShow() {
List<FrameData> lisCurrDataToShow = new ArrayList<FrameData>();
//for (FrameData fd : listFrameData) {//concurrent modification exception
//todo iterator test
Iterator<FrameData> iterator = listFrameData.iterator();
while (iterator.hasNext()) {
FrameData fd = iterator.next();
long currentTimeInMillis = java.lang.System.currentTimeMillis();
if ((currentTimeInMillis > fd.getStartDate().getTime() && currentTimeInMillis < fd.getEndDate().getTime()) || (fd.isAllDay() && DateUtils.isToday(fd.getStartDate().getTime()))) {
if (new File(ImageFrameActivity.ROOT_FOLDER_FILES + fd.getFileName()).exists()) {
lisCurrDataToShow.add(fd);
}
}
}
if (lisCurrDataToShow.size() == 0) {
lisCurrDataToShow.add(new FrameData(defaultFileName, null, null, null, String.valueOf(120), false));
}
return lisCurrDataToShow;
}
Referred to Detecting concurrent modifications?
Please help!
EDIT1:
This problem occurs rarely not everytime.
If a threads is accessing getCurrentDataToShow() and another thread tries to access this function what will the function return?? I'm new to multithreading , please guide
Edit 2
in oncreate following methods of singleton are called periodically
DataModelManager.getInstance().getCurrentDataToShow();
DataModelManager.getInstance().parseData(responseString);
Complete singleton class
public class DataModelManager {
private static DataModelManager dataModelManager;
private ImageFrameActivity imageFrameAct;
private String defaultFileName;
public List<FrameData> listFrameData = new ArrayList<FrameData>();
// public CopyOnWriteArrayList<FrameData> listFrameData= new CopyOnWriteArrayList<FrameData>();
private String screensaverName;
private boolean isToDownloadDeafultFiles;
private String tickerMsg = null;
private boolean showTicker = false;
private boolean showHotspot = false;
private String hotspotFileName=null;
public String getDefaultFileName() {
return defaultFileName;
}
public boolean isToDownloadDeafultFiles() {
return isToDownloadDeafultFiles;
}
public void setToDownloadDeafultFiles(boolean isToDownloadDeafultFiles) {
this.isToDownloadDeafultFiles = isToDownloadDeafultFiles;
}
private String fileNames;
private DataModelManager() {
}
public static DataModelManager getInstance() {
if (dataModelManager == null) {
synchronized (DataModelManager.class) {
if (dataModelManager == null) {
dataModelManager = new DataModelManager();
}
}
}
return dataModelManager;
}
private synchronized void addImageData(FrameData frameData) {
//Log.d("Frame Data","Start date "+frameData.getStartDate()+ " " +"end date "+frameData.getEndDate());
listFrameData.add(frameData);
}
public synchronized void parseData(String jsonStr) throws JSONException {
listFrameData.clear();
if (jsonStr == null) {
return;
}
List<String> listFileNames = new ArrayList<String>();
JSONArray jsonArr = new JSONArray(jsonStr);
int length = jsonArr.length();
for (int i = 0; i < length; i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
dataModelManager.addImageData(new FrameData(jsonObj.optString("filename", ""), jsonObj.optString("start", ""), jsonObj.optString("end", ""), jsonObj.optString("filetype", ""), jsonObj.optString("playTime", ""), jsonObj.optBoolean("allDay", false)));
listFileNames.add(jsonObj.optString("filename", ""));
}
fileNames = listFileNames.toString();
}
public void setDefaultFileData(String jsonStr) throws JSONException {
JSONObject jsonObj = new JSONObject(jsonStr);
defaultFileName = jsonObj.optString("default_image", "");
screensaverName = jsonObj.optString("default_screensaver ", "");
}
#Override
public String toString() {
return fileNames.replace("[", "").replace("]", "") + "," + defaultFileName + "," + screensaverName;
}
public FrameData getFrameData(int index) {
return listFrameData.get(index);
}
public synchronized List<FrameData> getCurrentDataToShow() {
List<FrameData> lisCurrDataToShow = new ArrayList<FrameData>();
// for (FrameData fd : listFrameData) {//concurrent modification exception
//todo iterator test
Iterator<FrameData> iterator = listFrameData.iterator();
while (iterator.hasNext()) {
FrameData fd = iterator.next();
long currentTimeInMillis = java.lang.System.currentTimeMillis();
if ((currentTimeInMillis > fd.getStartDate().getTime() && currentTimeInMillis < fd.getEndDate().getTime()) || (fd.isAllDay() && DateUtils.isToday(fd.getStartDate().getTime()))) {
if (new File(ImageFrameActivity.ROOT_FOLDER_FILES + fd.getFileName()).exists()) {
lisCurrDataToShow.add(fd);
}
}
}
if (lisCurrDataToShow.size() == 0) {
lisCurrDataToShow.add(new FrameData(defaultFileName, null, null, null, String.valueOf(120), false));
}
return lisCurrDataToShow;
}
public String getCurrentFileNames() {
String currFileNames = "";
List<FrameData> currFrameData = getCurrentDataToShow();
for (FrameData data : currFrameData) {
currFileNames += "," + data.getFileName();
}
return currFileNames;
}
public ImageFrameActivity getImageFrameAct() {
return imageFrameAct;
}
public void setImageFrameAct(ImageFrameActivity imageFrameAct) {
this.imageFrameAct = imageFrameAct;
}
}
This is the only part of your question that is currently answerable:
If a threads is accessing getCurrentDataToShow() and another thread tries to access this function what will the function return?
It depends on whether you are calling getCurrentDataToShow() on the same target object; i.e. what this is.
If this is the same for both calls, then the first call will complete before the second call starts.
If this is different, you will be locking on different objects, and the two calls could overlap. Two threads need to lock the same object to achieve mutual exclusion.
In either case, this method is not changing the listFrameData collection. Hence it doesn't matter whether the calls overlap! However, apparently something else is changing the contents of the collection. If that code is not synchronizing at all, or if it is synchronizing on a different lock, then that could be a source of problems.
Now you say that you are not seeing ConcurrentModificationException's at the moment. That suggests (but does not prove) that there isn't a synchronization problem at all. And that suggests (but does not prove) that your current problem is a logic error.
But (as I commented above) there are reasons to doubt that the code you have shown us is an true reflection of your real code. You need to supply an MVCE if you want a more definite diagnosis.