Coupling between classes - oop

Let's assume we have two classes: Patient and MedicalExamination. We want to get all examinations for a specific patient. Which one is better:
patient.getExaminations();
examination.get(patient);

How would you implement the second way? It would force you to loop over all examinations and see which one are about your patient.
The first method however will allow each patient to hold its own examinations and thus immediately retrieve it. Therefore my preference goes to this method.
Give the object as much information as you reasonably should (without lowering security concerns) so it can operate on its own.

Basically, an examination cannot exist without a patient. Therefore Patient would be the root entity of the aggregate Patient - MedicalExamination, and as such Patient (or PatientRepository) would be the way to retrieve patient details and examinations:
patient.getExaminations();

A doctor usually have a file for each patient, in each patient's file, there is a list of medical examination.
So, you get the patient from the doctor office, then you get the examination from the patient's file.
so:
patient.getExaminations();

Related

SQL Database Design Timesheet Employee vs Crews

First question here so please let me know how to ask the question better if below is unhelpful.
TLDR - Should I have separates employee times tables for employees assigned to crews and those who are not?
I'm trying to design database, following 'Database Design for mere mortals' book, that tracks employees times. I'm trying to replace the weekly timesheets and crew paper sheets (with start & end times for the crew) being used. There are also individual employee weekly timesheets for those not assigned to crews. Also crew sheets sometimes have an asterisk with if someone is sick etc.
There is a relationship of Projects to crews (1:N) and for the individual employee not assigned to crews are assigned to the project.
Employees are assigned to crews, normally 1:1 but headache comes when 1:N.
'Has' Relationships
So at the moment the are different types of crews say A, B, C, D, E.
Crews D & E will just fill in weekly timesheets (project, names and times, so crews D and E will both be on this same project) and the daily sheets don't include times. Sometimes like 10% of the time employees will be on both D & E on the same day.
A, B, C will have daily times on the daily sheet, but if an employee is on crew C these times take precedance over the times on sheets A or B (if they are also on A or B).
The obvious answer to have {employee, datetimestart, datetimeend} won't work as I care where the times have come from (crew, individual if exception to the crew e.g. sick, individual not assigned to a crew).
I can extend to have {employee, crewtype, datetimestart, datetimeend} this doesn't take care of when employee is both on D & E. I can put DE or F in this case?
Then how do I deal with those assigned to the project only?
if I have {employee, crewtype, projectref, datetimestart, datetimeend} the projectref is redudant and can be derived from crewtype when this is not null. Is this a reasonable approach or would having separate tables be better?
EDIT - or should I have one table {crewid, datetimestart, datetimeend} - derive the times for employees from the crew-employee relationship, and have a separate {employee, datetimestart, datetimeend, category} with category saying if exception (e.g sick) or non-assigned individual?
There are a lot of complicated rules in this description. If you want to create a schema that will never break these rules it will likely be a very complicated normalised schema. I would suggest that some of the rules will be best catered for in your application logic.
In terms of how you store the date, remember that you always need to cater for the exception cases, so something that happens 10% of the time, or 1% or one in a thousand still needs to be catered for, otherwise you just cant save the data.
I would tend to design for the most detailed level, which might be employee,crew,project,date,times, and possible also add an allocation column that defaults to 1, but could be 0.5 if the person is half allocated to two crews at the same time.
You could then write queries so that if a person is allocated to a crew, they get the crew time by default unless they have some overridden values, or whatever other rules you need.
Really, this is the sort of iterative modelling you would do with your business users as you tease out the design. Not always easy in a real-world scenario.
Sorry there is no definitive model here, but maybe a few tips to consider that might help along the way. Good luck with it.

The Specificity of Insertion Anomalies

I'm currently trying to understand the nuances of Insertion/Deletion/Modification anomalies in SQL.
Currently, the example I'm trying to understand is as follows:
ENROLLMENT
StudentID(PK) StudentName ClassID ClassName
111 Joe E1 English1
222 Bob E1 English1
333 Mary H1 History1
The problem the example wants me to answer is:
Which of the following causes an insertion anomaly?
with the answers being
Inserting a Student without a Class
and
Inserting a Class without a Student
I don't really understand why one of these answers is more right than the other, why, or how. It seems to me like either could be acceptable. Thanks in advance.
You need to think in terms of how data is added to a system naturalistically (i.e. what series of events occur in the real world).
In this case you would create a set of classes, prior to registration, and then create and assign students to them when they turned up to register.
You would be unlikely to create a set of students and then create and assign classes to each one.
A class might only be able to hold 30 students. How do you deal with any extra students who want to be registered for that class?
If you register 100 students and then decide to create classes, which subjects do you create?
Why do students decide to turn up to register? [Presumably because of the classes on offer.]
You can create as many classes as you're able to fit into your time-table. The number of students that actually register might mean a class is cancelled, but it has to exist in the first instance.
In summary, "Inserting a Student without a Class" would be more likely to cause an insertion anomaly.

Filtering user access based on tabled responses

I am building a "Survey" type application. The user answers a set of questions with pre-vetted answers.
Question: Where do you live?
Answers: England, Finland, Spain, France, Monrovia
The answers in this case would be in a DropDownList.
Once the user has completed the basic responses (location, age, sex etc) I would like to be able to prevent them accessing the rest of the survey based on their answers.
So for example, if they live anywhere but England I want to direct them to a page which says "Thanks, but Monrovian's can't complete this survey". I need my filtering to be user configurable (Table based) and I need to be able to have ANDs and ORs.
So one filter being the user MUST earn 100k+ a year.
Another being they must either live in Spain, or be female AND like model trains - "100k+ && (Spain || (Female && Trains))"
I would usually use Enums and bitmasking for this, but as my country list is 200+ items long, I can't think of a sensible way to store the filtering.
Hopefully I have made some sense and someone has a decent solution :)
I don't know if I can answer your question completely, but I'll try...
So, we have a bunch of Views that are only visible to the user if she previously chose some answers, like, she will see view#3 if she is older that 30, and view#4 if she is younger than 30 AND from China, and view#5 if she is older than 40 AND from Spain OR Italy, and so on...I want also to introduce the notion of **step**, and for each step we could have 1, 2, or more corresponding views. Each view should have a set of rules (like the ones above) that define if it is displayed or not.
How to create these rules? These rules could be simple instances of a Filter class/interface that, when asked, should return true/false. Like this:
interface Filter {
boolean apply();
}
Then you can create Filters like 'older than 30', 'from Spain', whatever. Remember that each view is configured with a set of rules (Filters) so it can answer yes/no if asked if it can display itself.
Next, how to apply these filters?
We could have a controller object that only knows about **steps** (each step can have one or more corresponding views, as I said), and, after the user pressed **next** at the current step, it should collect the answers and apply them against the rules attached to each view. Like, take the answers from step one, and apply them to all views from step two, and see which one matches (returns true). For example, at step two, you can have two separate views, one for young people, other for old people, and you apply the rules from each view to decide if you show the old or young view.
I could give you one code example, and you could also do research on your own, since I know nothing about your technical environment. I have used Google Guava's predicates on a similar problem and here it is: suppose we are dealing with Witch objects, and each of them has name(string), age(int) and spells(collection) attributes. If I have a list of witches and I need to sort them based on specific criteria, I can do:
// first I want to sort witches by age(natural ordering) then by spells,
// and then by name lexicographically
Ordering.natural()
.compound(new BySpellsWitchOrdering())
.compound(new ByNameWitchOrdering())
.sortedCopy(witchList);
The above line of code is going to take the witch list and return a list of sorted witches according to the criteria. Your situation is pretty similar.
Next, how to create the answers? For each view(page), you have possible answers, like, for view#1, you can have : age, sex, race, country. You can construct some answers, in the form of strings, ints, enums, and pass them to the controller, which in turn is going to apply them to each view corresponding to the next step.
As for how to store the rules in the database, as an example, you could have a column defining rule name (like, OLDER THAN) and one column for value, say, 30. Again, I do not know that much about your environment, and it is a really general issue, so I will stop here...

How to retrieve a specific object through a relationship in core data

Say I have 2 managed objects in my model: Department and Employee (as discussed in the Core Data Programming Guide). If I already have a specific department retrieved, I know I can get all employees in that department through
NSSet *departmentsEmployees = aDepartment.employees;
but what if I want to find a specific employee (e.g. with employeeId = 123) in that set, change one of its attributes, then save the change? How do I do that? Can I do a targeted query on the set? Or would I have to loop through each employee to find the one I want?
It seems that it would be better to try to find it in the employees NSSet instead of doing a whole new query to the entire data model because I already have a specific department.
Thank you
One way to narrow the search is to use -[NSSet filteredSetUsingPredicate:].

Organising resource (URI) in REST API

Scenario 1
In my web application say for there is a screen for adding an employee to system. As soon as user tabs after entering name of the employee, it generates the employee code automatically (which is the next field) based on some logic and already present records in the database.
Now I want to expose rest API for this application so that third party devs can build on top of it. So, I will have a resource called as /Employee which will respond for GET, PUT and DELETE verbs. But when a client needs to autofill the code, which is a GET operation, it will be on what resource? Should I make a new resource /EmployeeCodeFor/{Name} or I should get it on /Employee/{Name}/GenerateCode? If I go with /Employee/{Name}/GenerateCode then what about my resource to GET, PUT and DELETE for Employee i.e. actually /Employee/{Id}?
Scenario 2
Here lets take the case of a stackoverflow post. So lets say the resource would be /Post/{Id}. In the same way as in the previous example it lists me possible duplicate question as soon as I tab out of the Title field.
Again on what URL I should get those possible duplicates?
I can't think of more scenarios just now. But many such kind of scenarios may come up in real life application development. How to implement them in RESTful way?
Update on scenario 1
Code and Id are two different fields. Id is primary key, code can be duplicate across departments just to illustrate. Also to generate a code, name should be provided first. So, if user types a name "FirstName LastName" then server might generate FL003 as code assuming that there are already two more employees with firstname starting from F and lastname starting from L in the said department. Department can be identified based on the logged in user.
One way to allow the server an opportunity to pre-fill a bunch of elements in a new resource is to do
POST /Employees
{with empty body}
=>
201 Created
Location: http://example.org/employee/3443
<Employee Id="3443">
<Code>E1001</Code>
<FirstName></FirstName>
<LastName></LastName>
</Employee>
This gives the server one chance to provide default values. If you are looking for a more interactive way for the server to provide feedback during the input, I have another approach but it will take quite a bit more explaining.
Scenario 1
Let say your employee code is a unique identifier. In this case, to get it, you would allow the user to complete any field for the new employee and then make a POST. The server would generate the code and respond to the POST with a link to /Employee/{generated_code} which is the record for your newly created employee.
A GET on /Employee would return a list of all employees. A GET on /Employee/{a_code} will give you the employee detail.
Scenario 2
You could have some kind of query on the /Post collection like /Post?title_like={question_title}. A GET /Post?title_like=REST How to would return you a list of all questions containing "REST How to".