Here is the piece of code that's bothering me. It does the task but I want to optimize it if possible.
I have list of Entities
Config for each Entity based on ID.
From Config has Tips for each Entity
From Config has Rejects for each Entity
Rejects have ID for each Tip
I get ID of Tip to be reject, remove it from allItems and add it to removeItems
Map<String, String> removeItems = new HashMap<>();
Map<String, Pair<String, Config>> allItems = new HashMap<>();
for(final Entity entity : entities) {
final Config config = Configs
.get(entity.getId());
if (config == null || entity.getTxId() == null) {
continue;
}
if (config.getTips() != null) {
for (final Tip tip : config.getTips()) {
String currentId = entity.getId();
String currentTipId = tip.getTipId();
if(allItems.containsKey(currentTipId)) {
Pair<String, Config> item = allItems.get(currentTipId);
if(tip.getPriority() > item.getValue().getPriority()) {
removeItems.put(currentTipId, item.getKey());
allItems.put(currentTipId, new Pair(currentId, tip));
} else {
removeItems.put(currentTipId, currentId);
}
} else {
allItems.put(currentTipId, new Pair(currentId, tip));
}
List<String> rejects = tip.getRejects();
if(CollectionUtils.isEmpty(rejects)) {
continue;
}
for (String reject : rejects) {
Pair<String, Config> pair = allItems.get(reject);
if (null != pair) {
String rejectId = pair.getKey();
if (StringUtils.isNotEmpty(rejectId)) {
removeItems.put(reject, rejectId);
}
}
}
}
}
}
Related
I found article below to do in python.
https://docs.aws.amazon.com/textract/latest/dg/examples-export-table-csv.html
also I used article below to extract text.
https://docs.aws.amazon.com/textract/latest/dg/detecting-document-text.html
but above article helped to get only text, I also used function "block.getBlockType()"
of Block but none of block returned its type as "CELL" even tables are there in image/pdf.
Help me found java library similar to "boto3" to extract all tables.
What I did, I created models of each dataset in the json response and can use this models to build a table view in jsf.
public static List<TableModel> getTablesFromTextract(TextractModel textractModel) {
List<TableModel> tables = null;
try {
if (textractModel != null) {
tables = new ArrayList<>();
List<BlockModel> tableBlocks = new ArrayList<>();
Map<String, BlockModel> blockMap = new HashMap<>();
for (BlockModel block : textractModel.getBlocks()) {
if (block.getBlockType().equals("TABLE")) {
tableBlocks.add(block);
}
blockMap.put(block.getId(), block);
}
for (BlockModel blockModel : tableBlocks) {
Map<Long, Map<Long, String>> rowMap = new HashMap<>();
for (RelationshipModel relationship : blockModel.getRelationships()) {
if (relationship.getType().equals("CHILD")) {
for (String id : relationship.getIds()) {
BlockModel cell = blockMap.get(id);
if (cell.getBlockType().equals("CELL")) {
long rowIndex = cell.getRowIndex();
long columnIndex = cell.getColumnIndex();
if (!rowMap.containsKey(rowIndex)) {
rowMap.put(rowIndex, new HashMap<>());
}
Map<Long, String> columnMap = rowMap.get(rowIndex);
columnMap.put(columnIndex, getCellText(cell, blockMap));
}
}
}
}
tables.add(new TableModel(blockModel, rowMap));
}
System.out.println("row Map " + tables.toString());
}
} catch (Exception e) {
LOG.error("Could not get table from textract model", e);
}
return tables;
}
private static String getCellText(BlockModel cell, Map<String, BlockModel> blockMap) {
String text = "";
try {
if (cell != null
&& CollectionUtils.isNotEmpty(cell.getRelationships())) {
for (RelationshipModel relationship : cell.getRelationships()) {
if (relationship.getType().equals("CHILD")) {
for (String id : relationship.getIds()) {
BlockModel word = blockMap.get(id);
if (word.getBlockType().equals("WORD")) {
text += word.getText() + " ";
} else if (word.getBlockType().equals("SELECTION_ELEMENT")) {
if (word.getSelectionStatus().equals("SELECTED")) {
text += "X ";
}
}
}
}
}
}
} catch (Exception e) {
LOG.error("Could not get cell text of table", e);
}
return text;
}
TableModel to create the view from:
public class TableModel {
private BlockModel table;
private Map<Long, Map<Long, String>> rowMap;
public TableModel(BlockModel table, Map<Long, Map<Long, String>> rowMap) {
this.table = table;
this.rowMap = rowMap;
}
public BlockModel getTable() {
return table;
}
public void setTable(BlockModel table) {
this.table = table;
}
public Map<Long, Map<Long, String>> getRowMap() {
return rowMap;
}
public void setRowMap(Map<Long, Map<Long, String>> rowMap) {
this.rowMap = rowMap;
}
#Override
public String toString() {
return table.getId() + " - " + rowMap.toString();
}
I have something similar:
public class AnalyzeDocument {
public DocumentModel startProcess(byte[] content) {
Region region = Region.EU_WEST_2;
TextractClient textractClient = TextractClient.builder().region(region)
.credentialsProvider(EnvironmentVariableCredentialsProvider.create()).build();
return analyzeDoc(textractClient, content);
}
public DocumentModel analyzeDoc(TextractClient textractClient, byte[] content) {
try {
SdkBytes sourceBytes = SdkBytes.fromByteArray(content);
Util util = new Util();
Document myDoc = Document.builder().bytes(sourceBytes).build();
List<FeatureType> featureTypes = new ArrayList<FeatureType>();
featureTypes.add(FeatureType.FORMS);
featureTypes.add(FeatureType.TABLES);
AnalyzeDocumentRequest analyzeDocumentRequest = AnalyzeDocumentRequest.builder().featureTypes(featureTypes)
.document(myDoc).build();
AnalyzeDocumentResponse analyzeDocument = textractClient.analyzeDocument(analyzeDocumentRequest);
List<Block> docInfo = analyzeDocument.blocks();
// util.displayBlockInfo(docInfo);
PageModel pageModel = util.getTableResults(docInfo);
DocumentModel documentModel = new DocumentModel();
documentModel.getPages().add(pageModel);
Iterator<Block> blockIterator = docInfo.iterator();
while (blockIterator.hasNext()) {
Block block = blockIterator.next();
log.debug("The block type is " + block.blockType().toString());
}
return documentModel;
} catch (TextractException e) {
System.err.println(e.getMessage());
}
return null;
}
and this is the util file:
public PageModel getTableResults(List<Block> blocks) {
List<Block> tableBlocks = new ArrayList<>();
Map<String, Block> blockMap = new HashMap<>();
for (Block block : blocks) {
blockMap.put(block.id(), block);
if (block.blockType().equals(BlockType.TABLE)) {
tableBlocks.add(block);
log.debug("added table: " + block.text());
}
}
PageModel page = new PageModel();
if (tableBlocks.size() == 0) {
return null;
}
int i = 0;
for (Block table : tableBlocks) {
page.getTables().add(generateTable(table, blockMap, i++));
}
return page;
}
private TableModel generateTable(Block table, Map<String, Block> blockMap, int index) {
TableModel model = new TableModel();
Map<Integer, Map<Integer, String>> rows = getRowsColumnsMap(table, blockMap);
model.setTableId("Table_" + index);
for (Map.Entry<Integer, Map<Integer, String>> entry : rows.entrySet()) {
RowModel rowModel = new RowModel();
Map<Integer, String> value = entry.getValue();
for (int i = 0; i < value.size(); i++) {
rowModel.getCells().add(value.get(i));
}
model.getRows().add(rowModel);
}
return model;
}
private Map<Integer, Map<Integer, String>> getRowsColumnsMap(Block block, Map<String, Block> blockMap) {
Map<Integer, Map<Integer, String>> rows = new HashMap<>();
for (Relationship relationship : block.relationships()) {
if (relationship.type().equals(RelationshipType.CHILD)) {
for (String childId : relationship.ids()) {
Block cell = blockMap.get(childId);
if (cell != null) {
int rowIndex = cell.rowIndex();
int colIndex = cell.columnIndex();
if (rows.get(rowIndex) == null) {
Map<Integer, String> row = new HashMap<>();
rows.put(rowIndex, row);
}
rows.get(rowIndex).put(colIndex, getText(cell, blockMap));
}
}
}
}
return rows;
}
public String getText(Block block, Map<String, Block> blockMap) {
String text = "";
if (block.relationships() != null && block.relationships().size() > 0) {
for (Relationship relationship : block.relationships()) {
if (relationship.type().equals(RelationshipType.CHILD)) {
for (String childId : relationship.ids()) {
Block wordBlock = blockMap.get(childId);
if (wordBlock != null && wordBlock.blockType() != null) {
if (wordBlock.blockType().equals(BlockType.WORD))) {
text += wordBlock.text() + " ";
}
}
}
}
}
}
return text;
}
ExtractSourceQueryOB is an object that represents queries, a query could have a master query and in this case the master query should not be removed from the list, querySet contains strings of keys that represent queries that are required in this operation but it doesnt take account of the possibility of that query having a master. So i tried to loop through all queries checking that query has a master and in that case add that object (ExtractSourceQueryOB ) to the final list.
private List<ExtractSourceQueryOB> checkRequiredQueries(List<ExtractSourceQueryOB> extractSourceQueryList, ExtractElement extractElement) {
Set<ExtractSourceQueryOB> queryList = new HashSet();
Set<String> querySet = new HashSet();
fillUsedSymbolList(querySet, extractElement);
for(ExtractSourceQueryOB extractSourceQuery : extractSourceQueryList) {
if(extractSourceQuery.getMaster() != null ) {
for(ExtractSourceQueryOB extractSourceQuery2 : extractSourceQueryList) {
if(extractSourceQuery.getMaster().equals(extractSourceQuery2.getSymbol())){
queryList.add(extractSourceQuery2);
}
}
}
}
for (ExtractSourceQueryOB extractSourceQuery : extractSourceQueryList) {
for (String s : querySet) {
if (extractSourceQuery.getSymbol().equalsIgnoreCase(s)) {
queryList.add(extractSourceQuery);
}
}
}
return new ArrayList<>(queryList);
}
How can i simplify this method ?
You could eliminate the second for by combining that code with the first. Like,
for(ExtractSourceQueryOB extractSourceQuery : extractSourceQueryList) {
if(extractSourceQuery.getMaster() != null ) {
for(ExtractSourceQueryOB extractSourceQuery2 : extractSourceQueryList) {
if(extractSourceQuery.getMaster().equals(extractSourceQuery2.getSymbol())){
queryList.add(extractSourceQuery2);
}
}
}
for (String s : querySet) {
if (extractSourceQuery.getSymbol().equalsIgnoreCase(s)) {
queryList.add(extractSourceQuery);
}
}
}
private List<ExtractSourceQueryOB> checkRequiredQueries(List<ExtractSourceQueryOB> extractSourceQueryList, ExtractElement extractElement) {
Set<ExtractSourceQueryOB> queryList = new HashSet();
Set<String> querySet = new HashSet();
fillUsedSymbolList(querySet, extractElement);
for (ExtractSourceQueryOB extractSourceQuery : extractSourceQueryList) {
if (isyUsedAsMaster(extractSourceQuery,extractSourceQueryList) || isUsed(extractSourceQuery,querySet, extractSourceQueryList)) {
queryList.add(extractSourceQuery);
}
}
return new ArrayList<>(queryList);
}
private boolean isyUsedAsMaster(ExtractSourceQueryOB extractSourceQuery, List<ExtractSourceQueryOB> extractSourceQueryList) {
if (extractSourceQuery.getMaster() != null) {
for (ExtractSourceQueryOB extractSourceQuery2 : extractSourceQueryList) {
if (extractSourceQuery.getMaster().equals(extractSourceQuery2.getSymbol())) {
return true;
}
}
}
return false;
}
private boolean isUsed(ExtractSourceQueryOB extractSourceQuery, Set<String> querySet, List<ExtractSourceQueryOB> extractSourceQueryList) {
for (String s : querySet) {
if(extractSourceQuery.getSymbol().equalsIgnoreCase(s)) {
return true;
}
}
return false;
}
Previous answer above is correct but this is with some refactoring
This is an Android project. I'm completely new to Java (just started learning). As stated in the title, I'm getting an Incompatible Type Error
I've attached the respective method here :
public void init(Map map) {
this.productIds = new ArrayList();
try {
if (map.containsKey("products")) {
for (Entry<String, Object> "//Error Lies here" entry : ((HashMap) map.get("products")).entrySet()) {
InAppProduct productId = new InAppProduct();
productId.productId = ((String) entry.getKey()).toLowerCase();
HashMap<String, Object> extraValues = (HashMap) entry.getValue();
if (extraValues.containsKey(ShareConstants.MEDIA_TYPE)) {
productId.productType = (String) extraValues.get(ShareConstants.MEDIA_TYPE);
}
if (extraValues.containsKey("days")) {
productId.days = ((Integer) extraValues.get("days")).intValue();
}
this.productIds.add(productId);
}
return;
}
this.productIds = new ArrayList(ConfigurationFetcher.this.mDefaultsDelegate.getDefaultsInAppPackages());
} catch (Exception e) {
e.printStackTrace();
}
}
The Error is :
Required Object but found Entry <String, Object>
Let me know if you need additional code or any details. Thank You.
Set is a generic type. It is a container that can contain any kind of object.
In your case, it seems that your Set contains Map.Entry<String, Object> objects but since you don't specify that anywhere, Java assumes your Set contains Objects (the Java class that all other classes derive from) and produces an Incompatible Type Error.
Here's a slightly altered version of your code that should work.
public void init(Map map) {
this.productIds = new ArrayList();
try {
if (map.containsKey("products")) {
// ***** We now specify the type of object that the Set contains.
Set<Map.Entry<String, Object>> entrySet = ((HashMap) hm.get("products")).entrySet();
for (Entry<String, Object> entry : entrySet) {
InAppProduct productId = new InAppProduct();
productId.productId = ((String) entry.getKey()).toLowerCase();
HashMap<String, Object> extraValues = (HashMap) entry.getValue();
if (extraValues.containsKey(ShareConstants.MEDIA_TYPE)) {
productId.productType = (String) extraValues.get(ShareConstants.MEDIA_TYPE);
}
if (extraValues.containsKey("days")) {
productId.days = ((Integer) extraValues.get("days")).intValue();
}
this.productIds.add(productId);
}
return;
}
this.productIds = new ArrayList(ConfigurationFetcher.this.mDefaultsDelegate.getDefaultsInAppPackages());
} catch (Exception e) {
e.printStackTrace();
}
}
map.get("products")).entrySet() is a set of products, each product is a Object, not Entry <String, Object>.
This should work:
public void init(Map map) {
this.productIds = new ArrayList();
try {
if (map.containsKey("products")) {
for (Object entry : ((HashMap) map.get("products")).entrySet()) {
InAppProduct productId = new InAppProduct();
productId.productId = ((String) entry.getKey()).toLowerCase();
HashMap<String, Object> extraValues = (HashMap) entry.getValue();
if (extraValues.containsKey(ShareConstants.MEDIA_TYPE)) {
productId.productType = (String) extraValues.get(ShareConstants.MEDIA_TYPE);
}
if (extraValues.containsKey("days")) {
productId.days = ((Integer) extraValues.get("days")).intValue();
}
this.productIds.add(productId);
}
return;
}
this.productIds = new ArrayList(ConfigurationFetcher.this.mDefaultsDelegate.getDefaultsInAppPackages());
} catch (Exception e) {
e.printStackTrace();
}
}
I have following Structure in my firebase firestore
I want to get data of Form1 and Form2 how to achieve..
Below is what i tried
registration= query.whereEqualTo("UID", sharedPref.getString("userId",null)).addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
}
for (DocumentChange documentChange : documentSnapshots.getDocumentChanges()) {
if (documentChange.getDocument().getData().get("mobileMenus") != null) {
try {
Log.d("order","one");
String notesResponse = documentChange.getDocument().getData().get("mobileMenus").toString().replace(" ", "").replace(":", "");
String responseNotes = notesResponse.replace("=", ":");
Log.d("shownotes","***** "+responseNotes);
JSONObject jsonObject = new JSONObject(responseNotes);
isAttendance = jsonObject.getString("Attendance");
shared_menueditor.putString("isAttendance",isAttendance);
isCalender = jsonObject.getString("Calender");
shared_menueditor.putString("Calender",isCalender);
isExpenses = jsonObject.getString("Expenses");
shared_menueditor.putString("Expenses",isExpenses);
isleaves = jsonObject.getString("Leaves");
shared_menueditor.putString("Leaves",isleaves);
isLogout = jsonObject.getString("LogOut");
shared_menueditor.putString("LogOut",isLogout);
isNoticeboard = jsonObject.getString("NoticeBoard");
shared_menueditor.putString("NoticeBoard",isNoticeboard);
isTasks = jsonObject.getString("Tasks");
shared_menueditor.putString("Tasks",isTasks);
isTrackEmployee = jsonObject.getString("TrackEmployee");
shared_menueditor.putString("TrackEmployee",isTrackEmployee);
// documentChange.getDocument().getData().get("dynForms");
// Log.d("total", String.valueOf(documentChange.getDocument().getData().get("dynForms")));
Log.d("order","two");
forms= (Map<String, Object>) documentChange.getDocument().getData().get("mobileMenus");
Log.d("showfomsizes","*** "+forms.size());
} catch (JSONException e1) {
e1.printStackTrace();
}
}
if (forms!= null) {
for (Map.Entry<String, Object> form : forms.entrySet()) {
String key = form.getKey();
Map<Object, Object> values = (Map<Object, Object>) form.getValue();
name = (String) values.get("name");
String id = (String) values.get("id");
Log.d("nesteddata", name + "......" + id + "......." + key);
if (key.contains("Form1")) {
shared_menueditor.putString("nav_form1",name);
}
if (key.contains("Form2")) {
shared_menueditor.putString("nav_form2",name);
}
if (key.contains("Form3")) {
shared_menueditor.putString("nav_form3",name);
}
}
shared_menueditor.apply();
shared_menueditor.commit();
}
userprofile();
}
}
});
I am able to get Attendance,Calender,etc..But my pbm is i am not able
to get Form1 and Form2 datas
Error:java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.util.Map
You are getting that exception because you are not checking for the Boolean values that you have. This should be your code snippet for getting the forms. Your forms object is the entire mobileMenus object.
if (forms!= null) {
for (Map.Entry<String, Object> form : forms.entrySet()) {
String key = form.getKey();
if(key.contains("Form")) {
Map<Object, Object> values = (Map<Object, Object>) form.getValue();
name = (String) values.get("name");
String id = (String) values.get("id");
Log.d("nesteddata", name + "......" + id + "......." + key);
if (key.contains("Form1")) {
shared_menueditor.putString("nav_form1",name);
}
if (key.contains("Form2")) {
shared_menueditor.putString("nav_form2",name);
}
if (key.contains("Form3")) {
shared_menueditor.putString("nav_form3",name);
}
}
}
}
I am using java with mongodb.. I am getting performance issue in this below code can you please optimize this code?
I need to increase the performance of this function.
//first function which receives the parameter as associationId. can you guys please help me to increase the performance for this function..
public ArrayList<ResidentAssociationMapEntity> listUsersByAssociation(String
associationId) {
HashMap<String, Object> filterExpressions = new HashMap<String, Object>();
filterExpressions.put("associationId", associationId);
filterExpressions.put("residentStatus", 1);
List<ResidentAssociationMapEntity> residents =
DBManager.getInstance().getByMultipleFields(
ResidentAssociationMapEntity.class, filterExpressions);
if (residents != null && residents.size() > 0) {
for (ResidentAssociationMapEntity map : residents ) {
map.setUser(getUser(map.getUserId()));
}
}
return new ArrayList<ResidentAssociationMapEntity>(residents);
}
// get user function
public UserEntity getUser(String userId) {
UserEntity entity = DBManager.getInstance().getById(UserEntity.class,
userId);
ResidentAssociationMapEntity map =
getResidentAssocaitionMap(entity.getUserId());
if (map != null) {
entity.setUnitNo(map.getUnitNumber() + "");
entity.setAssociationId(map.getAssociationId());
entity.setAssociationName(CompanyDAO.getInstance().getCompany(map.getAssocia
tionId()).getName());
}
return entity;
}
//getResidentAssocaitionMap function
private ResidentAssociationMapEntity getResidentAssocaitionMap(String
residentId) {
HashMap<String, Object> filterExpressions = new HashMap<String, Object>();
filterExpressions.put("userId", residentId);
filterExpressions.put("residentStatus", 1);
List<ResidentAssociationMapEntity> residents =
DBManager.getInstance().getByMultipleFields(
ResidentAssociationMapEntity.class, filterExpressions);
if (residents != null && residents.size() > 0) {
return residents.get(0);
}
return null;
}