Skip to main content
summaryrefslogtreecommitdiffstats
blob: 28a9b5ea0a7e6054397fe9c51ec15ca0f038a298 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.validation;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.plugin.core.util.ExtensionDefinedObjects;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;
import org.eclipse.osee.framework.skynet.core.internal.Activator;

/**
 * @author Roberto E. Escobar
 */
public class OseeValidator {
   private static final String EXTENSION_ELEMENT = "OseeValidator";
   private static final String EXTENSION_ID = Activator.PLUGIN_ID + "." + EXTENSION_ELEMENT;
   private static final String CLASS_NAME_ATTRIBUTE = "classname";
   private final static OseeValidator instance = new OseeValidator();

   private final ExtensionDefinedObjects<IOseeValidator> loadedObjects;

   private OseeValidator() {
      loadedObjects =
         new ExtensionDefinedObjects<IOseeValidator>(EXTENSION_ID, EXTENSION_ELEMENT, CLASS_NAME_ATTRIBUTE);
   }

   public static OseeValidator getInstance() {
      return instance;
   }

   public IStatus validate(int requiredQualityOfService, Artifact artifact, String attributeTypeName, Object proposedValue) {
      IStatus status = Status.OK_STATUS;
      try {
         AttributeType attributeType = AttributeTypeManager.getType(attributeTypeName);
         status = validate(requiredQualityOfService, artifact, attributeType, proposedValue);
      } catch (Exception ex) {
         status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getLocalizedMessage(), ex);
      }
      return status;
   }

   public IStatus validate(int requiredQualityOfService, Artifact artifact, IAttributeType attributeType, Object proposedValue) {
      for (IOseeValidator validator : loadedObjects.getObjects()) {
         if (requiredQualityOfService >= validator.getQualityOfService()) {
            try {
               if (validator.isApplicable(artifact, attributeType)) {
                  try {
                     IStatus status = validator.validate(artifact, attributeType, proposedValue);
                     if (!status.isOK()) {
                        return status;
                     }
                  } catch (Exception ex) {
                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getLocalizedMessage(), ex);
                  }
               }
            } catch (Exception ex) {
               return new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getLocalizedMessage(), ex);
            }
         }
      }
      return Status.OK_STATUS;
   }

   public IStatus validate(int requiredQualityOfService, Artifact artifact) {
      try {
         for (AttributeType attributeType : artifact.getAttributeTypes()) {
            for (Attribute<?> attribute : artifact.getAttributes(attributeType)) {
               IStatus status = validate(requiredQualityOfService, artifact, attributeType, attribute.getValue());
               if (!status.isOK()) {
                  return status;
                  //                  String messageToUse =
                  //                        String.format("%s:[%s] - %s", artifact.getArtifactTypeName(), artifact.getDescriptiveName(),
                  //                              status.getMessage());
                  //                  if (status.isMultiStatus()) {
                  //                     MultiStatus mStatus =
                  //                           new MultiStatus(status.getPlugin(), status.getCode(), messageToUse, status.getException());
                  //                     mStatus.merge(status);
                  //                     return mStatus;
                  //                  } else {
                  //                     return new Status(status.getSeverity(), status.getPlugin(), status.getCode(), messageToUse,
                  //                           status.getException());
                  //                  }
               }
            }
         }
      } catch (Exception ex) {
         return new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getLocalizedMessage(), ex);
      }
      return Status.OK_STATUS;
   }
   //   private void checkExtensionsLoaded() {
   //      if (loadedObjects.isEmpty()) {
   //         List<IConfigurationElement> elements = ExtensionPoints.getExtensionElements(EXTENSION_ID, EXTENSION_ELEMENT);
   //         for (IConfigurationElement element : elements) {
   //            // TODO Implement dynamic attribute type validation chain definition
   //            //            IExtension extension = ((IExtension) element.getParent());
   //            //            String identifier = extension.getUniqueIdentifier();
   //            String attributeTypeName = element.getAttribute(ATTRIBUTE_TYPENAME);
   //            String className = element.getAttribute(CLASS_NAME_ATTRIBUTE);
   //            String bundleName = element.getContributor().getName();
   //
   //            if (Strings.isValid(bundleName) && Strings.isValid(className)) {
   //               try {
   //                  Bundle bundle = Platform.getBundle(bundleName);
   //                  Class<?> taskClass = bundle.loadClass(className);
   //                  IOseeValidator object = null;
   //                  try {
   //                     Method getInstance = taskClass.getMethod("getInstance", new Class[] {});
   //                     object = (IOseeValidator) getInstance.invoke(null, new Object[] {});
   //                  } catch (Exception ex) {
   //                     object = (IOseeValidator) taskClass.newInstance();
   //                  }
   //                  if (object != null) {
   //                     AttributeType attributeType = AttributeTypeManager.getType(attributeTypeName);
   //                     loadedObjects.put(attributeType, object);
   //                  }
   //               } catch (Exception ex) {
   //                  OseeLog.log(OseeActivator.class, Level.SEVERE, String.format("Unable to Load: [%s - %s]", bundleName,
   //                        className), ex);
   //               }
   //            }
   //         }
   //      }
   //   }

   // TODO Implement dynamic attribute type validation chain definition
   //   public List<IValidator> createValidateChain(String xml) {
   //      List<IValidator> validators = new ArrayList<IValidator>();
   //      return validators;
   //   }
}

Back to the top