diff options
author | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-05-06 14:55:26 +0200 |
---|---|---|
committer | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-05-06 14:56:27 +0200 |
commit | 0caabf90f1697b47eb14c56613fb1b13db0014f6 (patch) | |
tree | b9649d4b5c16c9516109a8b59b5724b1fa9b85b1 | |
parent | 1d8f1252dc00a71cfa890dd9984a9bcab205d97c (diff) | |
download | sonarqube-0caabf90f1697b47eb14c56613fb1b13db0014f6.tar.gz sonarqube-0caabf90f1697b47eb14c56613fb1b13db0014f6.zip |
fix quality flaws
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleNormalizer.java | 79 | ||||
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java | 52 |
2 files changed, 56 insertions, 75 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleNormalizer.java index 90a0d5c7a70..1768262bd76 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleNormalizer.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleNormalizer.java @@ -81,51 +81,64 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu } @Override - public UpdateRequest normalize(ActiveRuleKey key) throws Exception { + public UpdateRequest normalize(ActiveRuleKey key) { return normalize(activeRuleDao.getByKey(key)); } - public UpdateRequest normalize(ActiveRuleParamDto param, ActiveRuleKey key) throws Exception { - XContentBuilder document = jsonBuilder().startObject(); - document.startObject(RuleNormalizer.RuleField.ACTIVE.key()); - document.startObject(key.toString()); - document.startObject(ActiveRuleField.PARAMS.key()); - document.startObject(param.getKey()); - indexField(ActiveRuleParamField.VALUE.key(), param.getValue(), document); - document.endObject(); - document.endObject(); - document.endObject(); - document.endObject(); - document.endObject(); + public UpdateRequest normalize(ActiveRuleParamDto param, ActiveRuleKey key) { + try { + XContentBuilder document = jsonBuilder().startObject(); + document.startObject(RuleNormalizer.RuleField.ACTIVE.key()); + document.startObject(key.toString()); + document.startObject(ActiveRuleField.PARAMS.key()); + document.startObject(param.getKey()); + indexField(ActiveRuleParamField.VALUE.key(), param.getValue(), document); + document.endObject(); + document.endObject(); + document.endObject(); + document.endObject(); + document.endObject(); /* Creating updateRequest */ - UpdateRequest request = new UpdateRequest() - .doc(document); - request.docAsUpsert(true); - return request; - + UpdateRequest request = new UpdateRequest() + .doc(document); + request.docAsUpsert(true); + return request; + } catch (Exception e) { + throw new IllegalStateException(String.format("Could not normalize Object with key %s", key.toString()), e); + } } @Override - public UpdateRequest normalize(ActiveRuleDto rule) throws Exception { - XContentBuilder document = jsonBuilder().startObject(); + public UpdateRequest normalize(ActiveRuleDto rule) { + try { + + XContentBuilder document = jsonBuilder().startObject(); - document.startObject(RuleNormalizer.RuleField.ACTIVE.key()); - document.startObject(rule.getKey().toString()); - indexField(ActiveRuleField.OVERRIDE.key(), rule.doesOverride(), document); - indexField(ActiveRuleField.INHERITANCE.key(), rule.getInheritance(), document); - indexField(ActiveRuleField.PROFILE_ID.key(), rule.getProfileId(), document); - indexField(ActiveRuleField.SEVERITY.key(), rule.getSeverityString(), document); - indexField(ActiveRuleField.PARENT_ID.key(), rule.getParentId(), document); + ActiveRuleKey key = rule.getKey(); + if(key == null){ + throw new IllegalStateException("Cannot normalize ActiveRuleDto with null key"); + } + + document.startObject(RuleNormalizer.RuleField.ACTIVE.key()); + document.startObject(key.toString()); + indexField(ActiveRuleField.OVERRIDE.key(), rule.doesOverride(), document); + indexField(ActiveRuleField.INHERITANCE.key(), rule.getInheritance(), document); + indexField(ActiveRuleField.PROFILE_ID.key(), rule.getProfileId(), document); + indexField(ActiveRuleField.SEVERITY.key(), rule.getSeverityString(), document); + indexField(ActiveRuleField.PARENT_ID.key(), rule.getParentId(), document); /* Done normalizing for Rule */ - document.endObject(); - document.endObject(); + document.endObject(); + document.endObject(); /* Creating updateRequest */ - UpdateRequest request = new UpdateRequest() - .doc(document); - request.docAsUpsert(true); - return request; + UpdateRequest request = new UpdateRequest() + .doc(document); + request.docAsUpsert(true); + return request; + } catch (Exception e) { + throw new IllegalStateException(String.format("Could not normalize Object with key %s", rule.getKey().toString()), e); + } } } diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java b/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java index 1de481a5680..12680137ec7 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java @@ -38,15 +38,19 @@ public abstract class BaseNormalizer<E extends Dto<K>, K extends Serializable> { } } - public UpdateRequest normalizeOther(Object object, Object key) throws Exception { - return (UpdateRequest) this.getClass() - .getMethod("normalize", object.getClass(), key.getClass()) - .invoke(this, object, key); + public UpdateRequest normalizeOther(Object object, Object key) { + try { + return (UpdateRequest) this.getClass() + .getMethod("normalize", object.getClass(), key.getClass()) + .invoke(this, object, key); + } catch (Exception e) { + throw new IllegalStateException("Could not invoke Normalizer Method", e); + } } - public abstract UpdateRequest normalize(K key) throws IOException, Exception; + public abstract UpdateRequest normalize(K key); - public abstract UpdateRequest normalize(E dto) throws Exception; + public abstract UpdateRequest normalize(E dto); private static final Logger LOG = LoggerFactory.getLogger(BaseNormalizer.class); @@ -57,40 +61,4 @@ public abstract class BaseNormalizer<E extends Dto<K>, K extends Serializable> { LOG.error("Could not set {} to {} in ESDocument", field, value); } } - - -// protected void indexField(Fields field, Object dto, XContentBuilder document) { -// try { -// document.field(field.key(), field.method.invoke(dto)); -// } catch (IllegalArgumentException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } catch (IllegalAccessException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } catch (InvocationTargetException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// } -// -// -// -//private static Method getReadMethod(String method){ -// try { -// return RuleDto.class.getDeclaredMethod(method); -// } catch (SecurityException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } catch (NoSuchMethodException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// return null; -//} - - } |