private INakedObject GetValue(IObjectSpec specification, object rawValue) {
            if (rawValue == null) {
                return null;
            }

            if (specification.IsParseable) {
                return specification.GetFacet<IParseableFacet>().ParseTextEntry(rawValue.ToString(), framework.NakedObjectManager);
            }

            var no = framework.NakedObjectManager.CreateAdapter(rawValue, null, null);

            // the rawValue is not necessarily a collection so need extra check here to avoid 
            // a potential error getting the element spec. 
            if (specification.IsCollection && (no.Spec.IsCollection && !no.Spec.IsParseable)) {
                var elementSpec = specification.GetFacet<ITypeOfFacet>().GetValueSpec(no, framework.MetamodelManager.Metamodel);

                if (elementSpec.IsParseable) {
                    var elements = ((IEnumerable) rawValue).Cast<object>().Select(e => elementSpec.GetFacet<IParseableFacet>().ParseTextEntry(e.ToString(), framework.NakedObjectManager)).ToArray();
                    var elementType = TypeUtils.GetType(elementSpec.FullName);
                    Type collType = typeof (List<>).MakeGenericType(elementType);
                    var collection = framework.NakedObjectManager.CreateAdapter(Activator.CreateInstance(collType), null, null);
                    collection.Spec.GetFacet<ICollectionFacet>().Init(collection, elements);
                    return collection;
                }
            }


            return no;
        }
        private ITestObject GetBoundedInstance(string title, IObjectSpec spec)
        {
            if (spec.GetFacet <IBoundedFacet>() == null)
            {
                Assert.Fail(spec.SingularName + " is not a Bounded type");
            }
            IEnumerable allInstances = NakedObjectsFramework.Persistor.Instances(spec);
            var         inst         = allInstances.Cast <object>().Single(o => NakedObjectsFramework.NakedObjectManager.CreateAdapter(o, null, null).TitleString() == title);

            return(TestObjectFactoryClass.CreateTestObject(NakedObjectsFramework.NakedObjectManager.CreateAdapter(inst, null, null)));
        }
 private INakedObjectAdapter GetViewModel(string[] keys, IObjectSpec spec)
 {
     try {
         INakedObjectAdapter viewModel = framework.LifecycleManager.CreateViewModel(spec);
         spec.GetFacet <IViewModelFacet>().Populate(keys, viewModel, framework.NakedObjectManager, framework.DomainObjectInjector);
         return(viewModel);
     }
     catch (Exception e) {
         Log.Warn("View Model not found with exception", e);
         Log.WarnFormat("View Model not found keys: {0} type: {1}", keys == null ? "null" : keys.Aggregate("", (s, t) => s + " " + t), spec == null ? "null" : spec.FullName);
         return(null);
     }
 }
            private INakedObject[] GetConditionalList(INakedObject nakedObject, ArgumentsContext arguments) {
                Tuple<string, IObjectSpec>[] expectedParms = GetChoicesParameters();
                IDictionary<string, object> actualParms = arguments.Values;

                string[] expectedParmNames = expectedParms.Select(t => t.Item1).ToArray();
                string[] actualParmNames = actualParms.Keys.ToArray();

                if (expectedParmNames.Count() < actualParmNames.Count()) {
                    throw new BadRequestNOSException("Wrong number of conditional arguments");
                }

                if (!actualParmNames.All(expectedParmNames.Contains)) {
                    throw new BadRequestNOSException("Unrecognised conditional argument(s)");
                }

                Func<Tuple<string, IObjectSpec>, object> getValue = ep => {
                    if (actualParms.ContainsKey(ep.Item1)) {
                        return actualParms[ep.Item1];
                    }
                    return ep.Item2.IsParseable ? "" : null;
                };


                var matchedParms = expectedParms.ToDictionary(ep => ep.Item1, ep => new {
                    expectedType = ep.Item2,
                    value = getValue(ep),
                    actualType = getValue(ep) == null ? null : framework.MetamodelManager.GetSpecification(getValue(ep).GetType())
                });

                var errors = new List<ContextSurface>();

                var mappedArguments = new Dictionary<string, INakedObject>();

                foreach (var ep in expectedParms) {
                    string key = ep.Item1;
                    var mp = matchedParms[key];
                    object value = mp.value;
                    IObjectSpec expectedType = mp.expectedType;
                    ITypeSpec actualType = mp.actualType;

                    if (expectedType.IsParseable && actualType.IsParseable) {
                        string rawValue = value.ToString();

                        try {
                            mappedArguments[key] = expectedType.GetFacet<IParseableFacet>().ParseTextEntry(rawValue, framework.NakedObjectManager);

                            errors.Add(new ChoiceContextSurface(key, GetSpecificationWrapper(expectedType)) {
                                ProposedValue = rawValue
                            });
                        }
                        catch (Exception e) {
                            errors.Add(new ChoiceContextSurface(key, GetSpecificationWrapper(expectedType)) {
                                Reason = e.Message,
                                ProposedValue = rawValue
                            });
                        }
                    }
                    else if (actualType != null && !actualType.IsOfType(expectedType)) {
                        errors.Add(new ChoiceContextSurface(key, GetSpecificationWrapper(expectedType)) {
                            Reason = string.Format("Argument is of wrong type is {0} expect {1}", actualType.FullName, expectedType.FullName),
                            ProposedValue = actualParms[ep.Item1]
                        });
                    }
                    else {
                        mappedArguments[key] = framework.NakedObjectManager.CreateAdapter(value, null, null);

                        errors.Add(new ChoiceContextSurface(key, GetSpecificationWrapper(expectedType)) {
                            ProposedValue = getValue(ep)
                        });
                    }
                }

                if (errors.Any(e => !string.IsNullOrEmpty(e.Reason))) {
                    throw new BadRequestNOSException("Wrong type of conditional argument(s)", errors);
                }

                return GetChoices(nakedObject, mappedArguments);
            }
        public static INakedObjectAdapter GetTypedCollection(this INakedObjectsFramework framework, ISpecification featureSpec, IEnumerable collectionValue)
        {
            IObjectSpec collectionitemSpec = framework.MetamodelManager.GetSpecification(featureSpec.GetFacet <IElementTypeFacet>().ValueSpec);

            string[] rawCollection = collectionValue.Cast <string>().ToArray();
            object[] objCollection;

            Type instanceType    = TypeUtils.GetType(collectionitemSpec.FullName);
            var  typedCollection = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(instanceType));

            if (collectionitemSpec.IsParseable)
            {
                objCollection = rawCollection.Select(s => string.IsNullOrEmpty(s) ? null : collectionitemSpec.GetFacet <IParseableFacet>().ParseTextEntry(s, framework.NakedObjectManager).Object).ToArray();
            }
            else
            {
                // need to check if collection is actually a collection memento
                if (rawCollection.Count() == 1)
                {
                    INakedObjectAdapter firstObj = framework.GetNakedObjectFromId(rawCollection.First());

                    if (firstObj != null && firstObj.Oid is ICollectionMemento)
                    {
                        return(firstObj);
                    }
                }

                objCollection = rawCollection.Select(s => framework.GetNakedObjectFromId(s).GetDomainObject()).ToArray();
            }

            objCollection.Where(o => o != null).ForEach(o => typedCollection.Add(o));

            return(framework.NakedObjectManager.CreateAdapter(typedCollection.AsQueryable(), null, null));
        }
 public static INakedObjectAdapter Parse(this IObjectSpec spec, string s, INakedObjectsFramework framework)
 {
     return(s == null?framework.NakedObjectManager.CreateAdapter("", null, null) : spec.GetFacet <IParseableFacet>().ParseTextEntry(s, framework.NakedObjectManager));
 }
 private INakedObjectAdapter GetViewModel(string[] keys, IObjectSpec spec) {
     try {
         INakedObjectAdapter viewModel = framework.LifecycleManager.CreateViewModel(spec);
         spec.GetFacet<IViewModelFacet>().Populate(keys, viewModel, framework.NakedObjectManager, framework.DomainObjectInjector);
         return viewModel;
     }
     catch (Exception e) {
         Log.Warn("View Model not found with exception", e);
         Log.WarnFormat("View Model not found keys: {0} type: {1}", keys == null ? "null" : keys.Aggregate("", (s, t) => s + " " + t), spec == null ? "null" : spec.FullName);
         return null;
     }
 }