Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Compile time error on "Attributes attrs = results[0].getAttributes("");"

Nitinraj May 20, 2020

Hi ,

I have a scripted field and I am getting an error on

"Attributes attrs = results[0].getAttributes("");". 

Line no. 33 in below code

image.png 

The code runs properly and gives the correct result but I want to remove the error from the line.

 

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 22, 2020

That's not a compilation error... it's a "static type checking" error.

They are generally safe to ignore. It just means that the editor is not able to confirm that results[0] yield an object that has a "getAttributes()" method.

So to fix that (not that I would bother... this is groovy code, not java), you would have to look up the javadoc for the classes you are using to get the SearchResult.

Guessing at what the might be since you didn't show the import, I'm thinking this might be in the ballpark:

https://docs.oracle.com/javase/7/docs/api/javax/naming/directory/SearchResult.html#getAttributes()

If that's correct, then try:

Attributes attrs = results[0].getAttributes(); 

 (no empty quotes inside getAtrributes() since no arguments are requested for that method)

Nitinraj May 25, 2020

Hi @PD Sheehan ,

Thank you for your reply. Yes it is a static type error.

I have already tried :

Attributes attrs = results[0].getAttributes(); 

 

But it is not giving expected results. It returns null.

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 26, 2020

Can you share the full code (just remove/hide passwords or other binding/private information)?

What you seem to be telling me is that getAttributes("") works, returns the values you expect, but you don't like the static code checking error in the editor.

But when you remove the doubleQuotes, the static code checking errors go away, but the method returns null instead of the data you want.

My advice, in this case, would be to just ignore the static type errors.

But if you share the rest of the code, I can run a couple of tests in my environment and see what I get.

Nitinraj May 29, 2020

Hi @PD Sheehan ,

I don't want to ignore the static type errors. So I am finding the replacement methods.

Here is the full code.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
/////////////////////
@GrabResolver(name='apache-snapshot', root='http://repository.apache.org/snapshots/')
@Grab(group='org.apache.directory', module='groovyldap', version='0.1-SNAPSHOT')
import javax.naming.Context;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.LdapName;s
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.directory.groovyldap.*
////////////////////////////////////////
def userManager = ComponentAccessor.getUserManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cFieldError=customFieldManager.getCustomFieldObject("customfield_18010")

//def cFieldErrorOld = customFieldManager.getCustomFieldObject("customfield_18010")
//String cFieldErrorOldValue = issue.getCustomFieldValue(cFieldErrorOld)

String sAMAccountName
String displayError = null;
if (issue.reporter){
sAMAccountName = issue.reporter.name;
}else {
displayError = "Reporter: Anonymous";
cFieldError.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cFieldError), displayError), new DefaultIssueChangeHolder())
return null
}

def ldap = LDAP.newInstance('ldap://ldap.akad.kaercher.com:389/OU=KAERCHER,OU=ACCOUNTS,DC=AKAD,DC=KAERCHER,DC=COM','user','pw')

//get the direct manager and employee type of reporter
def attributesList = readAttributesFromPerson(ldap, sAMAccountName);
String cnManager = attributesList.get(0)
String employeeType = attributesList.get(1)
int numEmployeeType
String sAMAccountNameManager
String tempEmployeeType

if (employeeType == null && cnManager == null) {
displayError = "Employee Type Reporter (" + sAMAccountName + ") and Reporter Manager: null value from AD";
cFieldError.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cFieldError), displayError), new DefaultIssueChangeHolder())
return null
}else if (employeeType == null) {
displayError = "Employee Type Reporter (" + sAMAccountName + "): null value from AD";
cFieldError.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cFieldError), displayError), new DefaultIssueChangeHolder())
return null
}else if (cnManager == null) {
displayError = "Reporter Manager (" + sAMAccountName + "): null value from AD";
cFieldError.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cFieldError), displayError), new DefaultIssueChangeHolder())
return null
}else {
numEmployeeType = employeeType as Integer
if (numEmployeeType >= 4){
/*if (cFieldErrorOldValue != null && cFieldErrorOldValue.length() > 0) {
displayError = cFieldErrorOldValue + ", " + "There is no business unit manager";
}else {
displayError = "There is no business unit manager";
}*/
//displayError = "There is no business unit manager";
//cFieldError.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cFieldError), displayError), new DefaultIssueChangeHolder())
return null
}else{
String tempManager = cnManager;
int numTempEmployeeType = numEmployeeType
//repeat this loop till get a direct manager with employee type = 4
while(numTempEmployeeType < 4) {
sAMAccountNameManager = tempManager;
if (sAMAccountNameManager !=null) {
def attributesListManager = readAttributesFromPerson(ldap, sAMAccountNameManager);
tempManager = attributesListManager.get(0)
tempEmployeeType = attributesListManager.get(1)
if (tempEmployeeType != null && tempEmployeeType.length() > 0) {
numTempEmployeeType = tempEmployeeType as Integer
}else {
displayError = sAMAccountNameManager + "'s Employee Type: null value from AD";
break;
}
if (tempManager == null) {
displayError = sAMAccountNameManager + "'s Manager: null value from AD";
break;
}
}
else {
break;
}
}
if (numTempEmployeeType ==4) {
def userBusinessUnitManager = userManager.getUserByName(sAMAccountNameManager)
return userBusinessUnitManager
}else {
/*if (cFieldErrorOldValue != null && cFieldErrorOldValue.length() > 0) {
displayError = cFieldErrorOldValue + ", " + "There is no business unit manager";
}else {
displayError = "There is no business unit manager";
}*/
//displayError = "There is no business unit manager";
//cFieldError.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cFieldError), displayError), new DefaultIssueChangeHolder())
return null
}
}
}
///////////////////////////////////////////////////
//read attributes from LDAP in Jira using Groovy
List readAttributesFromPerson (LDAP ad, String sAMAccountName) {

def results = ad.search('(&(objectClass=person)(objectClass=user)(sAMAccountName='+sAMAccountName+'))', '', SearchScope.SUB )
String cnManager = null;
String employeeType = null;
if (results[0]){
Attributes attrs = results[0].getAttributes("");
String managerValueAttribute = attrs.get('manager');
String employeeTypeAttribute = attrs.get('employeeType');

if (managerValueAttribute != null && managerValueAttribute.length() > 0) {
String managerValue = managerValueAttribute.substring(9);
def listManagerValue = managerValue.split(',').collect{it as String}
cnManager = listManagerValue.get(0).substring(3)
}
if (employeeTypeAttribute != null && employeeTypeAttribute.length() > 0) {
employeeType = employeeTypeAttribute.substring(14);
}
}
List attributesList = [cnManager, employeeType]

return attributesList;
}

 

I try also tried with:

Instead of this:

def results = ad.search('(&(objectClass=person)(objectClass=user)(sAMAccountName='+sAMAccountName+'))', '', SearchScope.SUB )

I did:

ArrayList<SearchResult > results =(ArrayList<SearchResult>) ldap.search('(&(objectClass=person)(objectClass=user)(sAMAccountName='+sAMAccountNameManager+'))', '', SearchScope.SUB ) ;

 

But still the same error.

 

Your help will be appreciated.

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 29, 2020

Ah ok, this is the method you are calling

https://docs.oracle.com/javase/7/docs/api/javax/naming/directory/DirContext.html#getAttributes(java.lang.String)

I was able to get it to work without the static type checking errors by doing this:

def results = ad.search('(&(objectClass=person)(objectClass=user)(sAMAccountName='+sAMAccountName+'))', '', SearchScope.SUB ) 
if(results.size()>0){
def result = results[0] as LdapContext
Attributes attrs = result.getAttributes('');
}
Like Nitinraj likes this
Nitinraj May 31, 2020

Hi @PD Sheehan ,

Thanks for your help. It worked for me now.

Thanks again for your kind help :) 

Suggest an answer

Log in or Sign up to answer