Search in sources :

Example 1 with AbstractAttributeConverter

use of com.ramussoft.common.attribute.AbstractAttributeConverter in project ramus by Vitaliy-Yakovchuk.

the class VariantPlugin method getAttributeConverter.

@Override
public AttributeConverter getAttributeConverter() {
    return new AbstractAttributeConverter() {

        @SuppressWarnings("unchecked")
        public Object toObject(java.util.List<Persistent>[] persistents, long elementId, long attributeId, IEngine engine) {
            if (elementId < 0) {
                List list = persistents[0];
                if (list != null)
                    Collections.sort(list);
                return persistents[0];
            } else {
                List<Persistent> list = persistents[0];
                if (list.size() != 1)
                    return null;
                List<Persistent> ps = engine.getBinaryAttribute(-1, attributeId)[0];
                VariantPersistent vp = (VariantPersistent) list.get(0);
                for (Persistent p : ps) {
                    VariantPropertyPersistent pp = (VariantPropertyPersistent) p;
                    if (pp.getVariantId() == vp.getVariantId())
                        return pp.getValue();
                }
                return "WARNING: Unknown variant with variant id: " + vp.getVariantId();
            }
        }

        @SuppressWarnings("unchecked")
        @Override
        public List<Persistent>[] toPersistens(Object object, long elementId, long attributeId, IEngine engine) {
            if (elementId < 0) {
                List<VariantPropertyPersistent> list = (List) object;
                for (int i = 0; i < list.size(); i++) list.get(i).setPosition(i);
                return new List[] { list };
            } else {
                if (object == null) {
                    return new List[] { new ArrayList(0) };
                }
                List<Persistent> ps = engine.getBinaryAttribute(-1, attributeId)[0];
                VariantPersistent vp = new VariantPersistent();
                long maxId = -1;
                for (Persistent p : ps) {
                    VariantPropertyPersistent pp = (VariantPropertyPersistent) p;
                    if (pp.getValue().equals(object)) {
                        vp.setVariantId(pp.getVariantId());
                        List<Persistent> list = new ArrayList<Persistent>(1);
                        list.add(vp);
                        return new List[] { list };
                    }
                    if (maxId < pp.getVariantId())
                        maxId = pp.getVariantId();
                }
                VariantPropertyPersistent pp = new VariantPropertyPersistent();
                pp.setValue((String) object);
                pp.setVariantId(maxId + 1l);
                pp.setAttribute(attributeId);
                pp.setPosition(ps.size());
                vp.setVariantId(pp.getVariantId());
                if (engine instanceof Engine) {
                    ps.add(pp);
                    ((Engine) engine).setAttribute(null, engine.getAttribute(attributeId), ps);
                } else {
                    Transaction transaction = new Transaction();
                    transaction.getSave().add(pp);
                    engine.setBinaryAttribute(-1, attributeId, transaction);
                }
                List<Persistent> list = new ArrayList<Persistent>(1);
                list.add(vp);
                return new List[] { list };
            }
        }
    };
}
Also used : IEngine(com.ramussoft.common.IEngine) ArrayList(java.util.ArrayList) Persistent(com.ramussoft.common.persistent.Persistent) Transaction(com.ramussoft.common.persistent.Transaction) List(java.util.List) ArrayList(java.util.ArrayList) AbstractAttributeConverter(com.ramussoft.common.attribute.AbstractAttributeConverter) Engine(com.ramussoft.common.Engine) IEngine(com.ramussoft.common.IEngine)

Example 2 with AbstractAttributeConverter

use of com.ramussoft.common.attribute.AbstractAttributeConverter in project ramus by Vitaliy-Yakovchuk.

the class ProjectPreferencesPlugin method getAttributeConverter.

@Override
public AttributeConverter getAttributeConverter() {
    return new AbstractAttributeConverter() {

        @Override
        public Object toObject(List<Persistent>[] persistents, long elementId, long attributeId, IEngine engine) {
            ProjectOptions result;
            if (persistents[0].size() == 0)
                return null;
            else
                result = new ProjectOptions((IDEF0ModelPreferencesPersistent) persistents[0].get(0));
            for (Persistent p : persistents[1]) result.getReadedModel().addReaded().setDeligate((ReaderPersistent) p);
            return result;
        }

        @SuppressWarnings("unchecked")
        @Override
        public List<Persistent>[] toPersistens(Object object, long elementId, long attributeId, IEngine engine) {
            ProjectOptions po = (ProjectOptions) object;
            List<Persistent>[] res = new List[2];
            List<Persistent> f = new ArrayList<Persistent>();
            f.add(po.getDeligate());
            res[0] = f;
            List<Persistent> s = new ArrayList<Persistent>();
            for (Readed r : po.getReadedModel().getAllReaded()) {
                s.add(r.getDeligate());
            }
            res[1] = s;
            return res;
        }
    };
}
Also used : IEngine(com.ramussoft.common.IEngine) ArrayList(java.util.ArrayList) Persistent(com.ramussoft.common.persistent.Persistent) List(java.util.List) ArrayList(java.util.ArrayList) AbstractAttributeConverter(com.ramussoft.common.attribute.AbstractAttributeConverter) ProjectOptions(com.dsoft.pb.idef.elements.ProjectOptions) Readed(com.dsoft.pb.idef.elements.Readed)

Example 3 with AbstractAttributeConverter

use of com.ramussoft.common.attribute.AbstractAttributeConverter in project ramus by Vitaliy-Yakovchuk.

the class SectorPointPlugin method getAttributeConverter.

@Override
public AttributeConverter getAttributeConverter() {
    return new AbstractAttributeConverter() {

        @Override
        public Object toObject(List<Persistent>[] persistents, long elementId, long attributeId, IEngine engine) {
            List list = (List) persistents[0];
            Collections.sort(list);
            return persistents[0];
        }

        @Override
        public List<Persistent>[] toPersistens(Object object, long elementId, long attributeId, IEngine engine) {
            List<SectorPointPersistent> list = (List) object;
            for (int i = 0; i < list.size(); i++) list.get(i).setPosition(i);
            return new List[] { list };
        }
    };
}
Also used : IEngine(com.ramussoft.common.IEngine) Persistent(com.ramussoft.common.persistent.Persistent) List(java.util.List) AbstractAttributeConverter(com.ramussoft.common.attribute.AbstractAttributeConverter)

Example 4 with AbstractAttributeConverter

use of com.ramussoft.common.attribute.AbstractAttributeConverter in project ramus by Vitaliy-Yakovchuk.

the class ElementListPlugin method getAttributeConverter.

@Override
public AttributeConverter getAttributeConverter() {
    return new AbstractAttributeConverter() {

        @SuppressWarnings("unchecked")
        public Object toObject(java.util.List<Persistent>[] persistents, long elementId, long attributeId, IEngine engine) {
            if (elementId < 0) {
                return toPropertyObject(persistents);
            } else {
                List res = persistents[0];
                if (res != null)
                    Collections.sort(res);
                return res;
            }
        }

        @SuppressWarnings("unchecked")
        @Override
        public List<Persistent>[] toPersistens(Object object, long elementId, long attributeId, IEngine engine) {
            if (elementId < 0) {
                List<Persistent> list = new ArrayList<Persistent>(1);
                list.add((ElementListPropertyPersistent) object);
                return new List[] { list };
            } else
                return new List[] { new ArrayList<Persistent>(check(object, elementId, attributeId, engine)) };
        }

        @SuppressWarnings("unchecked")
        private List check(Object object, long elementId, long attributeId, IEngine engine) {
            List<ElementListPersistent> list = (List) object;
            ElementListPropertyPersistent p = (ElementListPropertyPersistent) toObject(engine.getBinaryAttribute(-1, attributeId), -1, attributeId, engine);
            if ((p.getQualifier1() == -1) || (p.getQualifier2() == -1))
                throw new RuntimeException("Left or right qualifier not seted.");
            long mq = engine.getQualifierIdForElement(elementId);
            if (mq == p.getQualifier1()) {
                for (int i = list.size() - 1; i >= 0; --i) {
                    ElementListPersistent e = list.get(i);
                    if ((e.getElement1Id() != elementId) || (engine.getQualifierIdForElement(e.getElement2Id())) != p.getQualifier2()) {
                        System.err.println("Element id not correct.");
                        list.remove(i);
                    }
                }
            } else if (mq == p.getQualifier2()) {
                for (int i = list.size() - 1; i >= 0; --i) {
                    ElementListPersistent e = list.get(i);
                    if ((e.getElement2Id() != elementId) || (engine.getQualifierIdForElement(e.getElement1Id())) != p.getQualifier1()) {
                        System.err.println("Element id not correct.");
                        list.remove(i);
                    }
                }
            } else {
                System.err.println("Element not correct.");
                return new ArrayList(0);
            }
            return list;
        }
    };
}
Also used : IEngine(com.ramussoft.common.IEngine) ArrayList(java.util.ArrayList) Persistent(com.ramussoft.common.persistent.Persistent) ArrayList(java.util.ArrayList) List(java.util.List) AbstractAttributeConverter(com.ramussoft.common.attribute.AbstractAttributeConverter)

Example 5 with AbstractAttributeConverter

use of com.ramussoft.common.attribute.AbstractAttributeConverter in project ramus by Vitaliy-Yakovchuk.

the class BooleanPlugin method getAttributeConverter.

@Override
public AttributeConverter getAttributeConverter() {
    return new AbstractAttributeConverter() {

        @SuppressWarnings("unchecked")
        @Override
        public List<Persistent>[] toPersistens(Object object, long elementId, long attributeId, IEngine engine) {
            if (object == null)
                return new List[] { new ArrayList<Persistent>(0) };
            ArrayList<Persistent> l = new ArrayList<Persistent>(1);
            l.add(new BooleanPersistent(((Boolean) object) ? 1 : 0));
            return new List[] { l };
        }

        @SuppressWarnings("unchecked")
        @Override
        public Object toObject(List<Persistent>[] persistents, long elementId, long attributeId, IEngine engine) {
            List l = persistents[0];
            if (l.size() == 0)
                return null;
            BooleanPersistent p = (BooleanPersistent) l.get(0);
            return p.getValue() != 0;
        }
    };
}
Also used : IEngine(com.ramussoft.common.IEngine) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Persistent(com.ramussoft.common.persistent.Persistent) AbstractAttributeConverter(com.ramussoft.common.attribute.AbstractAttributeConverter)

Aggregations

IEngine (com.ramussoft.common.IEngine)5 AbstractAttributeConverter (com.ramussoft.common.attribute.AbstractAttributeConverter)5 Persistent (com.ramussoft.common.persistent.Persistent)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 ProjectOptions (com.dsoft.pb.idef.elements.ProjectOptions)1 Readed (com.dsoft.pb.idef.elements.Readed)1 Engine (com.ramussoft.common.Engine)1 Transaction (com.ramussoft.common.persistent.Transaction)1