You need to sign in to do that
Don't have an account?
Charlie Chaplin 2017
Need some help with Apex class
Hi there,
To all my Devloper friends. I am facing a challenge at this moment. I have this Apex class fetching data from an custom Object and displaying it through a Visual force page. My problem is this class has been written by previous developer and i have no clue what is the logic behind this class to sort, match and display data on Visual force page.
It would be a great help if anyone can let me know how this class is matching data. What is the logic behind it and if i need to change this logic how can i do that.
Here is the Apex Class.
public class RegulationOCheckCtrl {
public class Wrapper {
public Boolean success { get; set; }
public Object message { get; set; }
public Object response { get; set; }
}
public class Match {
public String accountName { get; set; }
public Double similarity { get; set; }
}
public static List<String> tokenize(String str){
List<String> allPairs = new List<String>();
String[] tokens = str.split(' ');
for (Integer t = 0; t < tokens.size(); t++)
{
if (!String.isEmpty(tokens[t]))
{
String[] pairs = generateBigrams(tokens[t]);
for (Integer p = 0; p < pairs.size(); p++)
{
allPairs.add(pairs[p]);
}
}
}
return allPairs;
}
public static List<String> generateBigrams(String str){
Integer numPairs = str.length() - 1;
String[] bigrams = new String[numPairs];
System.debug(numPairs);
for(Integer i = 0; i < numPairs; i++){
bigrams[i] = str.substring(i, i + 2);
}
return bigrams;
}
public static String cleanString (String str){
String searchString = str;
String specialChars = '!|@|#|$|%|^|&|*|(|)|_|-|+|=|{|}|[|]|:|;|"|\'|<|>|,|.|?|/|\\|~|`';
for(Integer i = 0; i<specialChars.split('|').size(); i++){
searchString = searchString.replace(specialChars.split('|')[i], '');
}
return searchString;
}
@RemoteAction
public static Wrapper compare (Id accountId){
Wrapper w = new Wrapper();
try{
Account context = [SELECT Id, Name FROM Account WHERE Id = :accountId LIMIT 1];
List<String> source = tokenize(cleanString(context.Name.toUpperCase()));
List <Match> matches = new List<Match>();
for(RegOInsiders__c r: [SELECT Id, Name FROM RegOInsiders__c WHERE RecordType.DeveloperName = 'Current']){
List <String> target = tokenize(cleanString(r.Name.toUpperCase()));
Integer intersection = 0;
Integer union = source.size() + target.size();
for(Integer o = 0; o < source.size(); o++){
for(Integer i = 0; i < target.size(); i++){
if(source[o] == target[i]){
intersection++;
target.remove(i);
break;
}
}
}
Match m = new Match();
m.accountName = r.Name;
m.similarity = (2.0 * intersection) / union;
if(m.similarity !=0)
{
matches.add(m);
}
}
String[] messages = new String[]{'All Reg-O insiders compared and retrieved!'};
w.success = true;
w.message = messages;
w.response = matches;
}catch(Exception ex){
String[] messages = new String[]{ex.getMessage()};
w.success = false;
w.message = messages;
}
return w;
}
@RemoteAction
public static Wrapper record (Id accountId, String data){
Wrapper w = new Wrapper();
try{
Datetime d = Datetime.now();
String now = d.format('yyyy-MM-dd_HH-mm-ss');
Boolean match = false;
Account context = [SELECT Id, Name FROM Account WHERE Id = :accountId LIMIT 1];
List<Match> matches = (List<Match>)JSON.deserialize(data, List<Match>.class);
for(Match m: matches){
if(m.similarity > .80){
match = true;
}
}
Regulation_O_Check_History__c insertRegulationOCheckHistory = new Regulation_O_Check_History__c();
insertRegulationOCheckHistory.Associated_Account__c = context.Id;
insertRegulationOCheckHistory.Match__c = match;
INSERT insertRegulationOCheckHistory;
Regulation_O_Check_History__c regulationOCheckHistory = [SELECT Id, Name FROM Regulation_O_Check_History__c WHERE Id = :insertRegulationOCheckHistory.Id];
String csv = 'Comparing Against:,' + context.Name + '\n';
csv += '\n';
csv += 'Insider,% Similarity\n';
for(Match m: matches){
csv += m.accountName.escapeCSV() + ',' + m.similarity + '\n';
}
Blob blobCSV = Blob.valueOf(csv);
Attachment attachment = new Attachment();
attachment.ParentId = regulationOCheckHistory.Id;
attachment.Name = context.Name + '_' + regulationOCheckHistory.Name + '_' + now + '.csv';
attachment.ContentType = 'application/csv';
attachment.Body = blobCSV;
INSERT attachment;
String[] messages = new String[]{
'Created Reg-O Check History Record',
'/' + regulationOCheckHistory.Id
};
w.success = true;
w.message = messages;
}catch(Exception ex){
String[] messages = new String[]{ex.getMessage()};
w.success = true;
w.message = ex.getMessage();
}
return w;
}
public RegulationOCheckCtrl(ApexPages.StandardController ctrl){}
}
Your helpp is really appreciated
To all my Devloper friends. I am facing a challenge at this moment. I have this Apex class fetching data from an custom Object and displaying it through a Visual force page. My problem is this class has been written by previous developer and i have no clue what is the logic behind this class to sort, match and display data on Visual force page.
It would be a great help if anyone can let me know how this class is matching data. What is the logic behind it and if i need to change this logic how can i do that.
Here is the Apex Class.
public class RegulationOCheckCtrl {
public class Wrapper {
public Boolean success { get; set; }
public Object message { get; set; }
public Object response { get; set; }
}
public class Match {
public String accountName { get; set; }
public Double similarity { get; set; }
}
public static List<String> tokenize(String str){
List<String> allPairs = new List<String>();
String[] tokens = str.split(' ');
for (Integer t = 0; t < tokens.size(); t++)
{
if (!String.isEmpty(tokens[t]))
{
String[] pairs = generateBigrams(tokens[t]);
for (Integer p = 0; p < pairs.size(); p++)
{
allPairs.add(pairs[p]);
}
}
}
return allPairs;
}
public static List<String> generateBigrams(String str){
Integer numPairs = str.length() - 1;
String[] bigrams = new String[numPairs];
System.debug(numPairs);
for(Integer i = 0; i < numPairs; i++){
bigrams[i] = str.substring(i, i + 2);
}
return bigrams;
}
public static String cleanString (String str){
String searchString = str;
String specialChars = '!|@|#|$|%|^|&|*|(|)|_|-|+|=|{|}|[|]|:|;|"|\'|<|>|,|.|?|/|\\|~|`';
for(Integer i = 0; i<specialChars.split('|').size(); i++){
searchString = searchString.replace(specialChars.split('|')[i], '');
}
return searchString;
}
@RemoteAction
public static Wrapper compare (Id accountId){
Wrapper w = new Wrapper();
try{
Account context = [SELECT Id, Name FROM Account WHERE Id = :accountId LIMIT 1];
List<String> source = tokenize(cleanString(context.Name.toUpperCase()));
List <Match> matches = new List<Match>();
for(RegOInsiders__c r: [SELECT Id, Name FROM RegOInsiders__c WHERE RecordType.DeveloperName = 'Current']){
List <String> target = tokenize(cleanString(r.Name.toUpperCase()));
Integer intersection = 0;
Integer union = source.size() + target.size();
for(Integer o = 0; o < source.size(); o++){
for(Integer i = 0; i < target.size(); i++){
if(source[o] == target[i]){
intersection++;
target.remove(i);
break;
}
}
}
Match m = new Match();
m.accountName = r.Name;
m.similarity = (2.0 * intersection) / union;
if(m.similarity !=0)
{
matches.add(m);
}
}
String[] messages = new String[]{'All Reg-O insiders compared and retrieved!'};
w.success = true;
w.message = messages;
w.response = matches;
}catch(Exception ex){
String[] messages = new String[]{ex.getMessage()};
w.success = false;
w.message = messages;
}
return w;
}
@RemoteAction
public static Wrapper record (Id accountId, String data){
Wrapper w = new Wrapper();
try{
Datetime d = Datetime.now();
String now = d.format('yyyy-MM-dd_HH-mm-ss');
Boolean match = false;
Account context = [SELECT Id, Name FROM Account WHERE Id = :accountId LIMIT 1];
List<Match> matches = (List<Match>)JSON.deserialize(data, List<Match>.class);
for(Match m: matches){
if(m.similarity > .80){
match = true;
}
}
Regulation_O_Check_History__c insertRegulationOCheckHistory = new Regulation_O_Check_History__c();
insertRegulationOCheckHistory.Associated_Account__c = context.Id;
insertRegulationOCheckHistory.Match__c = match;
INSERT insertRegulationOCheckHistory;
Regulation_O_Check_History__c regulationOCheckHistory = [SELECT Id, Name FROM Regulation_O_Check_History__c WHERE Id = :insertRegulationOCheckHistory.Id];
String csv = 'Comparing Against:,' + context.Name + '\n';
csv += '\n';
csv += 'Insider,% Similarity\n';
for(Match m: matches){
csv += m.accountName.escapeCSV() + ',' + m.similarity + '\n';
}
Blob blobCSV = Blob.valueOf(csv);
Attachment attachment = new Attachment();
attachment.ParentId = regulationOCheckHistory.Id;
attachment.Name = context.Name + '_' + regulationOCheckHistory.Name + '_' + now + '.csv';
attachment.ContentType = 'application/csv';
attachment.Body = blobCSV;
INSERT attachment;
String[] messages = new String[]{
'Created Reg-O Check History Record',
'/' + regulationOCheckHistory.Id
};
w.success = true;
w.message = messages;
}catch(Exception ex){
String[] messages = new String[]{ex.getMessage()};
w.success = true;
w.message = ex.getMessage();
}
return w;
}
public RegulationOCheckCtrl(ApexPages.StandardController ctrl){}
}
Your helpp is really appreciated
That seems of an algorithm for finding out how many adjacent character pairs are contained in both strings (algorithms for similarity measure).
http://www.catalysoft.com/articles/StrikeAMatch.html
Previous article: Let me explain the algorithm by comparing the two strings 'France' and 'French'. First, I map them both to their upper case characters (making the algorithm insensitive to case differences), then split them up into their character pairs:
FRANCE: {FR, RA, AN, NC, CE}
FRENCH: {FR, RE, EN, NC, CH} ....
There are many algorithms for similarity measures: https://hpi.de/fileadmin/user_upload/fachgebiete/naumann/folien/SS13/DPDC/DPDC_12_Similarity.pdf
In the fields of computational linguistics and probability, an n-gram is a contiguous sequence of n items from a given sequence of text or speech:
https://en.wikipedia.org/wiki/N-gram
Best regards,
Alain