package com.burningcode.jira.issue.customfields.impl; import org.slf4j.LoggerFactory; import org.apache.commons.lang.StringUtils; import java.util.UUID; import webwork.action.ActionContext; import com.atlassian.jira.issue.fields.config.FieldConfig; import com.atlassian.jira.util.ErrorCollection; import com.atlassian.jira.issue.customfields.view.CustomFieldParams; import java.util.Comparator; import com.atlassian.jira.issue.comparator.ApplicationUserBestNameComparator; import java.util.ArrayList; import com.atlassian.plugin.webresource.WebResourceManager; import com.atlassian.jira.component.ComponentAccessor; import java.util.Map; import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem; import java.util.List; import com.atlassian.jira.issue.security.IssueSecurityLevel; import com.burningcode.jira.plugin.WatcherFieldContext; import com.atlassian.jira.project.Project; import com.atlassian.jira.permission.ProjectPermissions; import com.burningcode.jira.plugin.WatcherFieldSettings; import com.atlassian.jira.permission.GlobalPermissionKey; import javax.annotation.Nonnull; import com.atlassian.jira.issue.fields.CustomField; import java.util.Iterator; import com.atlassian.jira.user.ApplicationUser; import java.util.Collection; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.project.ProjectManager; import com.atlassian.jira.issue.fields.config.manager.FieldConfigSchemeManager; import com.atlassian.jira.template.soy.SoyTemplateRendererProvider; import com.atlassian.jira.security.roles.ProjectRoleManager; import com.atlassian.jira.security.groups.GroupManager; import com.atlassian.jira.issue.fields.rest.json.UserBeanFactory; import com.atlassian.jira.issue.fields.rest.json.beans.JiraBaseUrls; import com.atlassian.jira.web.FieldVisibilityManager; import com.atlassian.jira.bc.user.search.UserSearchService; import com.atlassian.jira.config.properties.ApplicationProperties; import com.atlassian.jira.issue.customfields.converters.MultiUserConverter; import com.atlassian.jira.issue.customfields.manager.GenericConfigManager; import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister; import com.atlassian.jira.user.UserFilterManager; import com.atlassian.jira.issue.watchers.WatcherManager; import com.atlassian.jira.security.PermissionManager; import com.atlassian.jira.security.JiraAuthenticationContext; import com.atlassian.jira.issue.security.IssueSecurityLevelManager; import com.atlassian.jira.security.GlobalPermissionManager; import org.slf4j.Logger; import com.atlassian.plugin.spring.scanner.annotation.component.Scanned; import com.atlassian.jira.issue.customfields.impl.MultiUserCFType; import com.atlassian.jira.config.FeatureManager; @Scanned public class WatcherFieldType extends MultiUserCFType { private static final Logger log; private final GlobalPermissionManager globalPermissionManager; private final IssueSecurityLevelManager issueSecurityLevelManager; private final JiraAuthenticationContext authenticationContext; private final PermissionManager permissionManager; private final WatcherManager watcherManager; private final UserFilterManager userFilterManager; public WatcherFieldType(@ComponentImport final CustomFieldValuePersister customFieldValuePersister, @ComponentImport final GenericConfigManager genericConfigManager, @ComponentImport final MultiUserConverter multiUserConverter, @ComponentImport final ApplicationProperties applicationProperties, @ComponentImport final JiraAuthenticationContext authenticationContext, @ComponentImport final UserSearchService searchService, @ComponentImport final FieldVisibilityManager fieldVisibilityManager, @ComponentImport final JiraBaseUrls jiraBaseUrls, @ComponentImport final UserBeanFactory userBeanFactory, @ComponentImport final GroupManager groupManager, @ComponentImport final ProjectRoleManager projectRoleManager, @ComponentImport final SoyTemplateRendererProvider soyTemplateRendererProvider, @ComponentImport final UserFilterManager userFilterManager, @ComponentImport final FieldConfigSchemeManager fieldConfigSchemeManager, @ComponentImport final ProjectManager projectManager, @ComponentImport final GlobalPermissionManager globalPermissionManager, @ComponentImport final PermissionManager permissionManager, @ComponentImport final WatcherManager watcherManager, @ComponentImport final IssueSecurityLevelManager issueSecurityLevelManager, @ComponentImport final FeatureManager featureManager) { super(customFieldValuePersister, genericConfigManager, multiUserConverter, applicationProperties, authenticationContext, searchService, fieldVisibilityManager, jiraBaseUrls, userBeanFactory, groupManager, projectRoleManager, soyTemplateRendererProvider, userFilterManager, fieldConfigSchemeManager, projectManager, featureManager); this.userFilterManager = userFilterManager; this.authenticationContext = authenticationContext; this.globalPermissionManager = globalPermissionManager; this.permissionManager = permissionManager; this.watcherManager = watcherManager; this.issueSecurityLevelManager = issueSecurityLevelManager; } protected void addWatchers(Issue issue, final Collection<ApplicationUser> userList) { if (userList != null && this.isIssueEditable(issue)) { for (final Object next : userList) { final ApplicationUser watcher = (ApplicationUser)next; if (!this.watcherManager.isWatching(watcher, issue)) { issue = this.watcherManager.startWatching(watcher, issue); } } } } public void createValue(final CustomField customField, final Issue issue, @Nonnull final Collection<ApplicationUser> value) { this.addWatchers(issue, value); } protected boolean isIssueEditable(final Issue issue) { return issue != null && issue.isCreated() && this.watcherManager.isWatchingEnabled() && this.isUserPermitted(issue); } public boolean isJiraAdmin(final ApplicationUser user) { return this.globalPermissionManager.hasPermission(GlobalPermissionKey.ADMINISTER, user); } public boolean isUserPermitted(final Issue issue) { final ApplicationUser user = this.getLoggedInUser(); if (WatcherFieldSettings.isIgnoreUserPermissions(user)) { return true; } if (issue == null) { WatcherFieldType.log.warn("WatcherFieldType#isUserPermitted was called with a null issue object. Should this have happened?"); return false; } final Project project = issue.getProjectObject(); if (project == null) { WatcherFieldType.log.warn("WatcherFieldType#isUserPermitted was called with an issue object with a null project. Should this have happened?"); return false; } return this.permissionManager.hasPermission(ProjectPermissions.MANAGE_WATCHERS, project, user); } public boolean isUserPermittedAsWatcher(final ApplicationUser user, final Project project) { return project != null && this.permissionManager.hasPermission(ProjectPermissions.BROWSE_PROJECTS, project, user); } public boolean isUserPermittedAsWatcher(final ApplicationUser user, final Issue issue) { return issue == null || this.permissionManager.hasPermission(ProjectPermissions.BROWSE_PROJECTS, issue, user); } public boolean hasIssueSecurityLevelPermissions(final ApplicationUser user, final WatcherFieldContext context) { final IssueSecurityLevel issueSecurityLevel = context.getIssueSecurityLevel(); if (issueSecurityLevel == null) { return true; } final Issue issue = context.getIssue(); if (issue != null) { return this.issueSecurityLevelManager.getUsersSecurityLevels(issue, user).contains(issueSecurityLevel); } return this.issueSecurityLevelManager.getUsersSecurityLevels(context.getProject(), user).contains(issueSecurityLevel); } public String getChangelogValue(final CustomField field, final Collection<ApplicationUser> value) { final List<ApplicationUser> watcherList = (List<ApplicationUser>)(List)value; if (watcherList == null || watcherList.isEmpty()) { return "None"; } String output = ""; final Iterator<ApplicationUser> i = watcherList.iterator(); while (i.hasNext()) { final ApplicationUser user = i.next(); if (user == null) { continue; } String displayName = user.getDisplayName(); if (displayName == null) { displayName = user.getName(); } output = output + displayName + (i.hasNext() ? ", " : ""); } return output; } public ApplicationUser getLoggedInUser() { return this.authenticationContext.getLoggedInUser(); } public Collection<ApplicationUser> getValueFromIssue(final CustomField field, final Issue issue) { if (!issue.isCreated()) { return (Collection<ApplicationUser>)super.getValueFromIssue(field, issue); } return this.getWatchers(issue); } @Nonnull public Map<String, Object> getVelocityParameters(final Issue issue, final CustomField field, final FieldLayoutItem fieldLayoutItem) { final Map<String, Object> params = (Map<String, Object>)super.getVelocityParameters(issue, field, fieldLayoutItem); params.put("hasPermission", false); if (issue == null || issue.getProjectObject() == null) { if (this.isJiraAdmin(this.getLoggedInUser())) { params.put("hasPermission", true); } } else if (this.isUserPermitted(issue)) { params.put("hasPermission", true); } if (WatcherFieldSettings.isHideDefaultWatcherList()) { final WebResourceManager webResourceManager = ComponentAccessor.getWebResourceManager(); webResourceManager.requireResource("com.burningcode.jira.issue.customfields.impl.jira-watcher-field:hide-resource"); } return params; } protected List<ApplicationUser> getWatchers(final Issue issue) { final List<ApplicationUser> currWatchers = new ArrayList<ApplicationUser>(this.watcherManager.getWatchers(issue, this.authenticationContext.getLocale())); currWatchers.sort((Comparator<? super ApplicationUser>)new ApplicationUserBestNameComparator()); return currWatchers; } protected void removeWatchers(Issue issue, final List<ApplicationUser> userList) { if (userList != null && this.isIssueEditable(issue)) { for (final Object next : userList) { final ApplicationUser user = (ApplicationUser)next; if (this.watcherManager.isWatching(user, issue)) { issue = this.watcherManager.stopWatching(user, issue); } } } } public void updateValue(final CustomField customField, final Issue issue, final Collection<ApplicationUser> value) { final List<ApplicationUser> newWatchers = (List<ApplicationUser>)(List)value; final List<ApplicationUser> currWatchers = this.getWatchers(issue); if (!currWatchers.isEmpty()) { if (newWatchers != null) { currWatchers.removeAll(newWatchers); } this.removeWatchers(issue, currWatchers); } this.addWatchers(issue, newWatchers); } public void validateFromParams(final CustomFieldParams relevantParams, final ErrorCollection errorCollectionToAddTo, final FieldConfig config) { final Collection<ApplicationUser> watchers = (Collection<ApplicationUser>)this.getValueFromCustomFieldParams(relevantParams); if (watchers != null && watchers.size() > 0) { if (!WatcherFieldSettings.ignoreBrowseIssuePermissions()) { final WatcherFieldContext context = new WatcherFieldContext(this.issueSecurityLevelManager, relevantParams, ActionContext.getParameters()); final Issue issue = context.getIssue(); final Project project = context.getProject(); final IssueSecurityLevel issueSecurityLevel = context.getIssueSecurityLevel(); if (project != null) { boolean hasSecurityLevel = false; if (issueSecurityLevel != null) { hasSecurityLevel = true; } final ArrayList<String> notPermittedUsers = new ArrayList<String>(); final String uuid = UUID.randomUUID().toString(); final boolean ignoreInactive = WatcherFieldSettings.isIgnoreDeactivatedWatcher(); for (final ApplicationUser user : watchers) { if (ignoreInactive && !user.isActive()) { WatcherFieldType.log.warn("[" + uuid + "] Ignore deactivated watchers enabled. Allowing deactivated user '" + user.getUsername() + "' to be watcher."); } else if (WatcherFieldSettings.isIgnoreDeactivatedWatcher(user)) { WatcherFieldType.log.warn("[" + uuid + "] Ignore deactivated watchers enabled. Allowing deactivated user '" + user.getUsername() + "' to be watcher."); } else { final boolean projectPerm = this.isUserPermittedAsWatcher(user, project); final boolean securityPerm = this.hasIssueSecurityLevelPermissions(user, context); final boolean issuePerm = this.isUserPermittedAsWatcher(user, issue); if (!projectPerm) { notPermittedUsers.add(user.getName()); } else if (hasSecurityLevel) { if (securityPerm) { continue; } notPermittedUsers.add(user.getName()); } else { if (issue == null || !project.getId().equals(issue.getProjectId()) || issuePerm) { continue; } notPermittedUsers.add(user.getName()); } } } if (notPermittedUsers.size() > 0) { errorCollectionToAddTo.addError(config.getFieldId(), "Users do not have permission to view this issue: " + StringUtils.join((Collection)notPermittedUsers, ", "), ErrorCollection.Reason.FORBIDDEN); } } } else { WatcherFieldType.log.debug("'Ignore browse issue permissions' enabled"); } } } public boolean valuesEqual(final Collection<ApplicationUser> v1, final Collection<ApplicationUser> v2) { final ArrayList<ApplicationUser> watcherList1 = (v1 != null) ? ((ArrayList)v1) : new ArrayList<ApplicationUser>(); final ArrayList<ApplicationUser> watcherList2 = (v2 != null) ? ((ArrayList)v2) : new ArrayList<ApplicationUser>(); watcherList1.sort((Comparator<? super ApplicationUser>)new ApplicationUserBestNameComparator()); watcherList2.sort((Comparator<? super ApplicationUser>)new ApplicationUserBestNameComparator()); return watcherList1.equals(watcherList2); } static { log = LoggerFactory.getLogger((Class)WatcherFieldType.class); } }
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast. The editor shows sample boilerplate code when you choose language as Java and start coding.
OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs. Following is a sample program that shows reading STDIN ( A string in this case ).
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String inp = input.next();
System.out.println("Hello, " + inp);
}
}
OneCompiler supports Gradle for dependency management. Users can add dependencies in the build.gradle
file and use them in their programs. When you add the dependencies for the first time, the first run might be a little slow as we download the dependencies, but the subsequent runs will be faster. Following sample Gradle configuration shows how to add dependencies
apply plugin:'application'
mainClassName = 'HelloWorld'
run { standardInput = System.in }
sourceSets { main { java { srcDir './' } } }
repositories {
jcenter()
}
dependencies {
// add dependencies here as below
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}
Java is a very popular general-purpose programming language, it is class-based and object-oriented. Java was developed by James Gosling at Sun Microsystems ( later acquired by Oracle) the initial release of Java was in 1995. Java 17 is the latest long-term supported version (LTS). As of today, Java is the world's number one server programming language with a 12 million developer community, 5 million students studying worldwide and it's #1 choice for the cloud development.
short x = 999; // -32768 to 32767
int x = 99999; // -2147483648 to 2147483647
long x = 99999999999L; // -9223372036854775808 to 9223372036854775807
float x = 1.2;
double x = 99.99d;
byte x = 99; // -128 to 127
char x = 'A';
boolean x = true;
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Example:
int i = 10;
if(i % 2 == 0) {
System.out.println("i is even number");
} else {
System.out.println("i is odd number");
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(<conditional-expression>) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations is known in advance.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(<condition>){
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (<condition>);
Class is the blueprint of an object, which is also referred as user-defined data type with variables and functions. Object is a basic unit in OOP, and is an instance of the class.
class
keyword is required to create a class.
class Mobile {
public: // access specifier which specifies that accessibility of class members
string name; // string variable (attribute)
int price; // int variable (attribute)
};
Mobile m1 = new Mobile();
public class Greeting {
static void hello() {
System.out.println("Hello.. Happy learning!");
}
public static void main(String[] args) {
hello();
}
}
Collection is a group of objects which can be represented as a single unit. Collections are introduced to bring a unified common interface to all the objects.
Collection Framework was introduced since JDK 1.2 which is used to represent and manage Collections and it contains:
This framework also defines map interfaces and several classes in addition to Collections.
Collection | Description |
---|---|
Set | Set is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc |
List | List is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors |
Queue | FIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue. |
Deque | Deque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail) |
Map | Map contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc. |